Need an example of invoke byref

Sep 14, 2009 at 6:57 AM

Hi, I wonder if anyone can give me an example on invoking a method with byref parameter. When I tried to invoke the method using expression constructed from Expression.Invoke(), I got the error "Expression of type 'System.Object&' cannot be used for parameter of type 'System.Object'". I wonder what I need to do to change an argument into a byref argument. Thanks.

 

Coordinator
Sep 14, 2009 at 6:24 PM

It should look like this:

var dict = Expression.Parameter(typeof(Dictionary<object, object>), “dict”);

var value = Expression.Parameter(typeof(object), “value”);

Expression.Block(

new[] { dict, value },

Expression.Call(

                dict,

                                typeof(Dictionary<object, object>).GetMethod(“TryGetValue”),

                                value

                )

);

                               

Here I’m just passing a variable in for the out-param of Dictionary<object, object>.TryGetValue.  You can also pass fields, array indexes, etc…  as well.  (Note: compiled w/ Outlook).

From: dotneteer [mailto:notifications@codeplex.com]
Sent: Sunday, September 13, 2009 10:58 PM
To: Dino Viehland
Subject: Need an example of invoke byref [dlr:68817]

From: dotneteer

Hi, I wonder if anyone can give me an example on invoking a method with byref parameter. When I tried to invoke the method using expression constructed from Expression.Invoke(), I got the error "Expression of type 'System.Object&' cannot be used for parameter of type 'System.Object'". I wonder what I need to do to change an argument into a byref argument. Thanks.

Read the full discussion online.

To add a post to this discussion, reply to this email (dlr@discussions.codeplex.com)

To start a new discussion for this project, email dlr@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Sep 15, 2009 at 6:50 AM

Thanks, Dino. It worked. Call with byref args is actually pretty transparent. The error that I got is from my callsite binder. I just need to use ParameterType.GetElementType() if the my parameter is byref.