How do I pass a ref parameter dynamically using Reflection

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
Let me see the code you have and I will point you in the direction. Are you
using invoke to run the method???
 
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???
 
Back
Top