Delegate Question

B

Bob Cramer

I don't have a copy of Reflector handy :-( so I'd appreciate if someone
could confirm (or clarify) the following.

In consideration of the following delegate declaration...

delegate string MyLovelyDelegate(int parm1, string parm2);


1. Is it true that, in the output assembly, a new class will be created that
inherits from System.MulticastDelegate - and the name of that class is
"MyLovelyDelegate". Yes?

2. Where in the output class (in the output assembly) do the parameters from
my delegate declaration go? Do they go into the constructor of the output
class? Or do they go into the signature of Invoke() and BeginInvoke()?

I appreciate any clarification you can provide.
 
J

Jon Skeet [C# MVP]

Bob Cramer said:
I don't have a copy of Reflector handy :-( so I'd appreciate if someone
could confirm (or clarify) the following.

In consideration of the following delegate declaration...

delegate string MyLovelyDelegate(int parm1, string parm2);

1. Is it true that, in the output assembly, a new class will be created that
inherits from System.MulticastDelegate - and the name of that class is
"MyLovelyDelegate". Yes?
Yes.

2. Where in the output class (in the output assembly) do the parameters from
my delegate declaration go? Do they go into the constructor of the output
class? Or do they go into the signature of Invoke() and BeginInvoke()?

I appreciate any clarification you can provide.

The signatures of Invoke and BeginInvoke.
 
B

Bob Cramer

Jon Skeet said:
The signatures of Invoke and BeginInvoke.


Thanks Jon... now a quick followup question. Where do Invoke and BeginInvoke
come from? I was just in MSDN looking at System.Delegate and
MulticastDelegate and those classes do not apparently have those two
methods. Does the compiler insert those methods into the output delegate
class when it is creating the new output class (e.g., MyLovelyDelegate)?
 
P

Peter Duniho

Bob said:
Thanks Jon... now a quick followup question. Where do Invoke and BeginInvoke
come from? I was just in MSDN looking at System.Delegate and
MulticastDelegate and those classes do not apparently have those two
methods. Does the compiler insert those methods into the output delegate
class when it is creating the new output class (e.g., MyLovelyDelegate)?

Yes. See http://msdn2.microsoft.com/en-us/library/system.delegate.aspx

Specifically:

Compilers provide two additional methods to the
delegate: BeginInvoke and EndInvoke. For more
information on these methods, see Asynchronous
Programming Overview.

Same thing applies to Invoke.

That's assuming you're talking about the methods on the Delegate class.

There's also Control.Invoke and Control.BeginInvoke. But those don't
include the parameters explicitly in their signatures; it's up to the
caller to put the parameters in an array that represents the parameter
list. So I'm guessing that's not what you meant.

Pete
 
B

Bob Cramer

Peter Duniho said:
Yes. See http://msdn2.microsoft.com/en-us/library/system.delegate.aspx

Specifically:

Compilers provide two additional methods to the
delegate: BeginInvoke and EndInvoke. For more
information on these methods, see Asynchronous
Programming Overview.

Same thing applies to Invoke.

That's assuming you're talking about the methods on the Delegate class.

There's also Control.Invoke and Control.BeginInvoke. But those don't
include the parameters explicitly in their signatures; it's up to the
caller to put the parameters in an array that represents the parameter
list. So I'm guessing that's not what you meant.

Pete

Thanks Pete... yes - I'm referring specifically to BeginInvoke on delegates.
And I did read the link carefully. I am, however confused about the absence
of an Invoke method on the delegate classes. I thought that delegates
offered an Invoke method - but it is clearly not there. I understand that to
invoke a delegate asynchronously we call BeginInvoke and eventulally
EndInvoke. But to invoke a delegate synchronously, we just specify the
delegate instance like we would any method name it references.... i.e., no
".Invoke" method on the delegate instance.

Did delegates ever (perhaps in an early .NET framework version) have a
..Invoke method for delegates (for synchronous invocation)? Am I missing
something or perhaps misguided on that fact?

Thanks again.
 
P

Peter Duniho

Bob said:
[...]
Did delegates ever (perhaps in an early .NET framework version) have a
..Invoke method for delegates (for synchronous invocation)? Am I missing
something or perhaps misguided on that fact?

They still have it, and yes it is synonymous with just using the "call a
method" syntax for the delegate. It just doesn't show up in the
documentation (actually, it might be in the C# specification
somewhere...I haven't bothered to look).

You'll see it in VS with Intellisense if you try it.

Pete
 
J

Jon Skeet [C# MVP]

Peter Duniho said:
Bob said:
[...]
Did delegates ever (perhaps in an early .NET framework version) have a
..Invoke method for delegates (for synchronous invocation)? Am I missing
something or perhaps misguided on that fact?

They still have it, and yes it is synonymous with just using the "call a
method" syntax for the delegate. It just doesn't show up in the
documentation (actually, it might be in the C# specification
somewhere...I haven't bothered to look).

You'll see it in VS with Intellisense if you try it.

It's not in the documentation because it's not in Delegate or
MultiCastDelegate - and can't be. What would the signature be?
SomeDelegate.Invoke has a signature matching the signature you've
declared for the method.

The only equivalent thing in Delegate itself is DynamicInvoke.
 

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