Whats with DynamicInvoke?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi to all.
Whats the difference between DynamicInvoke and Invoke methods of the
Delegate class?
Thanks in advance.
 
Hi Amir,

The difference between the Invoke and DynamicInvoke is Invoke requires the
target object that instantiated the method as parameter to execute the method.

But DynamicInvoke doesn't require the target object without which it calls
the method dynamically with the parameter list.

If the method is static then null can be passed as target object in Invoke
during which Invoke is similar to that of DynamicInvoke.



Invoke

Delegate objdel = new Delegate (this. Meth1);
Objdel.Method.Invoke (targetobject, new object [] {"params"});

If the method is static then
Objdel.Method.Invoke (null, new object [] {"params"});

DynamicInvoke
Objdel.DynamicInvoke (new object [] params);


Mihir Solanki
http://www.mihirsolanki.com
 
So why do I need both of them?
Why can't I always use Invoke method?
Is there a substantial difference between the two?

Mihir Solanki said:
Hi Amir,

The difference between the Invoke and DynamicInvoke is Invoke requires the
target object that instantiated the method as parameter to execute the method.

But DynamicInvoke doesn't require the target object without which it calls
the method dynamically with the parameter list.

If the method is static then null can be passed as target object in Invoke
during which Invoke is similar to that of DynamicInvoke.



Invoke

Delegate objdel = new Delegate (this. Meth1);
Objdel.Method.Invoke (targetobject, new object [] {"params"});

If the method is static then
Objdel.Method.Invoke (null, new object [] {"params"});

DynamicInvoke
Objdel.DynamicInvoke (new object [] params);


Mihir Solanki
http://www.mihirsolanki.com
Hi to all.
Whats the difference between DynamicInvoke and Invoke methods of the
Delegate class?
Thanks in advance.
 
Mihir Solanki said:
The difference between the Invoke and DynamicInvoke is Invoke requires the
target object that instantiated the method as parameter to execute the method.

But DynamicInvoke doesn't require the target object without which it calls
the method dynamically with the parameter list.

If the method is static then null can be passed as target object in Invoke
during which Invoke is similar to that of DynamicInvoke.

Invoke

Delegate objdel = new Delegate (this. Meth1);
Objdel.Method.Invoke (targetobject, new object [] {"params"});

That's not calling the delegate's Invoke method. It's calling the
MethodInfo's Invoke method.

Here's an example calling a delegates *actual* Invoke method:

using System;

public class Test
{
static void Main()
{
EventHandler x = new EventHandler(Foo);
x.Invoke("hello", null);
}

static void Foo (object sender, EventArgs e)
{
Console.WriteLine (sender);
}
}

The difference between Invoke and DynamicInvoke is that the parameters
to Invoke depend on the delegate itself - the method has the same
signature as the delegate. DynamicInvoke has a "fixed" signature, and
it dynamically calls Invoke with the appropriate parameters.
 
Back
Top