PC Review


Reply
Thread Tools Rate Thread

Delegate Question: How does it know?

 
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      7th Sep 2006
Learning about delegates (again, I admit), I think I finally get it,
maybe.

I can reference any method in any class in the same namespace as long
as it has the same signature. Right?

But how does it know? Does the .NET runtime keep a list of all the
possible methods that could be called and then when the delegate is
invoked, just look for a match? For example:

A very basic example (SimpleDelegate1.cs):

using System;

namespace Akadia.BasicDelegate
{
// Declaration
public delegate void SimpleDelegate();

class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("I was called by delegate ...");
}

public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

// Invocation
simpleDelegate();
}
}
}

Compile an test:

# csc SimpleDelegate1.cs
# SimpleDelegate1.exe
I was called by delegate ...

(taken from:
http://www.akadia.com/services/dotne...nd_events.html)

The system just "knows" to call MyFunc because it "knows" that delegate
can work with that type of method as defined by its signature?

 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      7th Sep 2006

Maybe this can help out.

This is the actual "register for an event" code.

The keyword being GetInvocationList





protected static MessageArrivalHandler m_handler;

public event MessageArrivalHandler MessageArrival
{
//when new handler is register for the event, start the listener
//if it is not yet started
add
{
m_handler += value;
}
remove
{
m_handler -= value;
//stop the listener if no handler is listed
if (m_handler == null || m_handler.GetInvocationList().Length <= 0)
{
//nobody is registered
}
}
}






<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Learning about delegates (again, I admit), I think I finally get it,
> maybe.
>
> I can reference any method in any class in the same namespace as long
> as it has the same signature. Right?
>
> But how does it know? Does the .NET runtime keep a list of all the
> possible methods that could be called and then when the delegate is
> invoked, just look for a match? For example:
>
> A very basic example (SimpleDelegate1.cs):
>
> using System;
>
> namespace Akadia.BasicDelegate
> {
> // Declaration
> public delegate void SimpleDelegate();
>
> class TestDelegate
> {
> public static void MyFunc()
> {
> Console.WriteLine("I was called by delegate ...");
> }
>
> public static void Main()
> {
> // Instantiation
> SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
>
> // Invocation
> simpleDelegate();
> }
> }
> }
>
> Compile an test:
>
> # csc SimpleDelegate1.cs
> # SimpleDelegate1.exe
> I was called by delegate ...
>
> (taken from:
> http://www.akadia.com/services/dotne...nd_events.html)
>
> The system just "knows" to call MyFunc because it "knows" that delegate
> can work with that type of method as defined by its signature?
>



 
Reply With Quote
 
Noah Sham
Guest
Posts: n/a
 
      7th Sep 2006
The delegate knows to call MyFunc because you told it to with the following
line of code.
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
This code creates an instance of the SimpleDelegate class and that instance
stores the reference to the static method MyFunc.

What may be confusing is that this line of code
public delegate void SimpleDelegate();
is doing two things: declaring the method signature of the delegate void
(void) and declaring a class called SimpleDelegate which the compiler
derives from MultiCastDelegate. Check out
'http://msdn2.microsoft.com/en-us/library/system.delegate.aspx for a
detailed explanation.


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Learning about delegates (again, I admit), I think I finally get it,
> maybe.
>
> I can reference any method in any class in the same namespace as long
> as it has the same signature. Right?
>
> But how does it know? Does the .NET runtime keep a list of all the
> possible methods that could be called and then when the delegate is
> invoked, just look for a match? For example:
>
> A very basic example (SimpleDelegate1.cs):
>
> using System;
>
> namespace Akadia.BasicDelegate
> {
> // Declaration
> public delegate void SimpleDelegate();
>
> class TestDelegate
> {
> public static void MyFunc()
> {
> Console.WriteLine("I was called by delegate ...");
> }
>
> public static void Main()
> {
> // Instantiation
> SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
>
> // Invocation
> simpleDelegate();
> }
> }
> }
>
> Compile an test:
>
> # csc SimpleDelegate1.cs
> # SimpleDelegate1.exe
> I was called by delegate ...
>
> (taken from:
> http://www.akadia.com/services/dotne...nd_events.html)
>
> The system just "knows" to call MyFunc because it "knows" that delegate
> can work with that type of method as defined by its signature?
>



 
Reply With Quote
 
Fao, Sean
Guest
Posts: n/a
 
      7th Sep 2006
If you look at a delegate as a pointer to a function (or in the case of
C#, a reference to a method) everything else should make sense.

Hope that helps,

--
Sean
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Sep 2006
<(E-Mail Removed)> wrote:
> Learning about delegates (again, I admit), I think I finally get it,
> maybe.
>
> I can reference any method in any class in the same namespace as long
> as it has the same signature. Right?


Namespaces are irrelevant. As of .NET 2.0, the signature doesn't have
to be exactly the same, just compatible.

> But how does it know? Does the .NET runtime keep a list of all the
> possible methods that could be called and then when the delegate is
> invoked, just look for a match? For example:


No - when you create a delegate instance, you tell it which method you
want to call. It remembers.

See http://www.pobox.com/~skeet/csharp/events.html for a closer look at
events and delegates.

> The system just "knows" to call MyFunc because it "knows" that delegate
> can work with that type of method as defined by its signature?


No, it knows to call MyFunc because you've created a delegate instance
passing MyFunc as the parameter:

SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Ben Voigt
Guest
Posts: n/a
 
      8th Sep 2006
"Fao, Sean" <(E-Mail Removed)-WANT-NO-SPAM> wrote in message
news:(E-Mail Removed)...
> If you look at a delegate as a pointer to a function (or in the case of
> C#, a reference to a method) everything else should make sense.


Actually, although P/Invoke automatically compiles delegates into function
pointers, that may not be the internal representation of a delegate. By
reading the discussion of delegates and constrained execution regions
(http://msdn.microsoft.com/msdnmag/is...0/Reliability/), it seems that
delegates are not pre-jitted.

So the original poster was very nearly correct. The runtime maintains a
list of all loaded methods (irregardless of namespace, and including all
assemblies). The delegate constructor checks the compatibility of the given
method (the C# compiler will do this in advance where possible), and stores
the metadata handle. Invoking the delegate causes the runtime to lookup the
method by its handle, JIT the MSIL if that hasn't been done yet, and then
run the native code generated by the JIT.

But nowhere does the runtime maintain a list of "methods compatible with
delegate type XYZ". It has a list of all methods, and enforces a subtype
constraint during compile or delegate construction (for construction by
reflection, see Delegate.CreateDelegate).

>
> Hope that helps,
>
> --
> Sean



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delegate question ^MisterJingo^ Microsoft C# .NET 4 14th Dec 2005 11:14 PM
Delegate question bigbob Microsoft VB .NET 0 31st Jan 2005 08:06 PM
delegate question manuel aldana Microsoft C# .NET 0 10th Jan 2005 07:20 PM
Delegate question M.Posseth Microsoft VB .NET 3 6th Jan 2005 03:30 PM
AddressOf/Delegate question clarified - VB6.0, .NET question BoloBaby Microsoft VB .NET 3 17th May 2004 12:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:06 AM.