Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2007

Tip: Looking for answers? Try searching our database.

Is it possbile to enumerate Methods by Event?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
athos.jingle@gmail.com - 23 Nov 2007 18:29 GMT
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.
Jon Skeet [C# MVP] - 23 Nov 2007 19:15 GMT
> 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".

Signature

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

athos.jingle@gmail.com - 23 Nov 2007 20:36 GMT
> 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
[quoted text clipped - 3 lines]
> Jon Skeet - <sk...@pobox.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.GetResourceString("InvalidOperation_NoPublicAddMethod"));
   }
   addMethod.Invoke(target, new object[] { handler });
}

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

public abstract MethodInfo GetAddMethod(bool nonPublic);
Jon Skeet [C# MVP] - 23 Nov 2007 20:46 GMT
> > 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
[quoted text clipped - 3 lines]
> 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.

> 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.

Signature

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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.