Delegate.DynamicInvoke Method

G

Guest

How can I pass, by ref or out parameter values when calling DynamicInvoke
Method.

e.g. Here is my Delegate:
public delegate int BinaryOp(int x, int y, out double z, ref bool overflow);

Here is how I am creating a delegate object for "Subtract" method.
BinaryOp op2 = (BinaryOp) Delegate.CreateDelegate(dt, tt,"Subtract");

Here is how I am creating parameters object[]. Notice t and ret are
variables that I want to pass by ref and out. The following statement works
but ofcourse I am not passing them by ref or value here, so it's giving me
desired results:

object[] param = {3,4,t,ret};
Console.WriteLine(op2.DynamicInvoke(param));

I know I can use invoke method to get solve this issue but I want to see how
can I use DynamicInvoke. e.g. Using invoke it would be:

Console.WriteLine (op2(3,4, out t, ref ret));

Thanks in Advance
Arif
 
N

Nicholas Paldino [.NET/C# MVP]

Arif,

I don't understand what the problem is. The object array should contain
the modified parameters (assuming they were modified when the method is
called). I just tested it now, and it works fine. If you check the object
array that you passed in, the elements at the indexes for the parameters
that are ref or out will be populated with the changed values (assuming they
are changed).

Hope this helps.
 

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