By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,679 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,679 IT Pros & Developers. It's quick & easy.

Whats with DynamicInvoke?

P: n/a
Hi to all.
Whats the difference between DynamicInvoke and Invoke methods of the
Delegate class?
Thanks in advance.
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Nov 22 '05 #2

P: n/a
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" wrote:
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.


Nov 22 '05 #3

P: n/a
Mihir Solanki <mi**********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.