HighTechTalks DotNet Forums  

Is it possbile to enumerate Methods by Event?

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Is it possbile to enumerate Methods by Event? in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
athos.jingle@gmail.com
 
Posts: n/a

Default Is it possbile to enumerate Methods by Event? - 11-23-2007 , 01:29 PM






To attach a method to an event, we can use EventInfo.AddEventHandler.

After attached, given an event's name, is it possible to retrieve all
methods attached to it?

Thank you.

Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Is it possbile to enumerate Methods by Event? - 11-23-2007 , 02:15 PM






athos.jingle (AT) gmail (DOT) com <athos.jingle (AT) gmail (DOT) com> wrote:
Quote:
To attach a method to an event, we can use EventInfo.AddEventHandler.

After attached, given an event's name, is it possible to retrieve all
methods attached to it?
No. An event is really just a pair of methods, add and remove. Although
*usually* there's a delegate variable backing it, the add/remove can do
anything they want, and there's no part of an event which is a "fetch".

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #3  
Old   
athos.jingle@gmail.com
 
Posts: n/a

Default Re: Is it possbile to enumerate Methods by Event? - 11-23-2007 , 03:36 PM



On Nov 23, 8:15 pm, Jon Skeet [C# MVP] <sk... (AT) pobox (DOT) com> wrote:
Quote:
No. An event is really just a pair of methods, add and remove. Although
*usually* there's a delegate variable backing it, the add/remove can do
anything they want, and there's no part of an event which is a "fetch".

--
Jon Skeet - <sk... (AT) pobox (DOT) com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
OK I'm just curious...
Yeah I could not find a way to track, but, it should be somewhere in
the memory, right? Existing as a linked list, or inside an internal
Class... Unfortunately I don't know CIL (MSIL), and reflector stops at
the mysterious abstract Method "abstract MethodInfo GetAddMethod":

[DebuggerHidden, DebuggerStepThrough]
public void AddEventHandler(object target, Delegate handler)
{
MethodInfo addMethod = this.GetAddMethod();
if (addMethod == null)
{
throw new
InvalidOperationException(Environment.GetResourceS tring("InvalidOperation_NoPublicAddMethod"));
}
addMethod.Invoke(target, new object[] { handler });
}

public MethodInfo GetAddMethod()
{
return this.GetAddMethod(false);
}

public abstract MethodInfo GetAddMethod(bool nonPublic);


Reply With Quote
  #4  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: Is it possbile to enumerate Methods by Event? - 11-23-2007 , 03:46 PM



athos.jingle (AT) gmail (DOT) com <athos.jingle (AT) gmail (DOT) com> wrote:
Quote:
On Nov 23, 8:15 pm, Jon Skeet [C# MVP] <sk... (AT) pobox (DOT) com> wrote:
No. An event is really just a pair of methods, add and remove. Although
*usually* there's a delegate variable backing it, the add/remove can do
anything they want, and there's no part of an event which is a "fetch".

OK I'm just curious...
Yeah I could not find a way to track, but, it should be somewhere in
the memory, right?
Usually, but not necessarily. For instance, you *could* write an event
that did nothing with the event handlers that you added:

public event EventHandler Foo
{
add {}
remove {}
}

Or here's an oddity - it adds to one delegate variable or another,
depending on the time of day (and removes from both):

EventHandler foo1;
EventHandler foo2;

public event EventHandler Foo
{
add
{
if (DateTime.Now.Hour >= 12)
{
foo1 += value;
}
else
{
foo2 += value;
}
}

remove
{
foo1 -= value;
foo2 -= value;
}
}

I'm not saying it's *sensible* (and in particular the removal side is
nasty) but it's valid code as far as C# and the CLR are concerned.

Quote:
Existing as a linked list, or inside an internal
Class... Unfortunately I don't know CIL (MSIL), and reflector stops at
the mysterious abstract Method "abstract MethodInfo GetAddMethod":
It's not mysterious - it finds which method is called when you add an
event handler.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.