Delegate Question

Delegate Question

am 17.10.2007 23:25:50 von Peter Schwartz

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.

Re: Delegate Question

am 17.10.2007 23:44:39 von skeet

Bob Cramer wrote:
> 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.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: Delegate Question

am 18.10.2007 00:29:30 von Peter Schwartz

"Jon Skeet [C# MVP]" wrote in message
news:MPG.2180929e6e94d91c553@msnews.microsoft.com...
> Bob Cramer wrote:
>> 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.
>


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)?

Re: Delegate Question

am 18.10.2007 00:50:01 von Peter Duniho

Bob Cramer wrote:
> 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.asp x

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

Re: Delegate Question

am 18.10.2007 01:43:11 von Peter Schwartz

"Peter Duniho" wrote in message
news:13hd4cqa4a1d7da@corp.supernews.com...
> Bob Cramer wrote:
>> 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.asp x
>
> 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.

Re: Delegate Question

am 18.10.2007 02:52:25 von Peter Duniho

Bob Cramer wrote:
> [...]
> 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

Re: Delegate Question

am 18.10.2007 08:40:17 von skeet

Peter Duniho wrote:
> Bob Cramer wrote:
> > [...]
> > 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.

--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too