How do I pass a ref parameter dynamically using Reflection

G

Guest

I need to use reflection to dynamically call a method which takes a ref
parameter. How do I create the appropriate object to pass as the ref
parameter - which presumably needs to be a reference to (address of ?) the
actual object.

For example, if I pass a simple value type, it would provide a copy to the
method which will not have the desired effect of changing the source value
itself.

Thanks for your help.

Robert.
 
S

Steve Flitcroft

Let me see the code you have and I will point you in the direction. Are you
using invoke to run the method???
 
S

Steve Flitcroft

Just include the local variable in the method parameter list for the method
you invoke. After the method has run get the variable back out of the array
and it should reflect the result of the method



Robert Matheson said:
I don't actually have any code for this yet, but yes, I am planning to use
MethodInfo.Invoke(object, object[]) in which the object[] contains the
parameters.

As a simple example of what I need to do, if I have a method M(ref int v)
which I want to call dynamically with M(ref variableName) - where
variableName is a local variable in the calling method - I assume that
somehow I need to pass in a reference to variableName rather than it's
current value such that its value gets updated by the method.

Robert.

Steve Flitcroft said:
Let me see the code you have and I will point you in the direction. Are you
using invoke to run the method???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top