6

Possible Duplicate:
Is it possible to “steal” an event handler from one control and give it to another?

Is there a way to get references of the event handlers from the event?

For example:

EventHandler evt = btn.Click; // or another way ?

(Here the EventHandler is the delegate and Click is an event of Button)

Community
  • 1
  • 1
Brij
  • 11,731
  • 22
  • 78
  • 116
  • Just wondering what you are trying to achieve?.... – Steve B Oct 09 '12 at 13:50
  • 1
    An event only implements add and remove accessor blocks, like get and set only different. As such, there is no "get" for events. [More info here](http://msdn.microsoft.com/en-us/library/awbftdfh.aspx) – Jasper Oct 09 '12 at 13:56
  • Found answer at http://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth see post from Hans Passant – Brij Nov 17 '12 at 14:48
  • 1
    @user125697 I found answer in the suggested duplicate. See the answer from Hans Passant. http://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth – Brij Apr 10 '13 at 09:21

2 Answers2

2

If you defined the event and your code that accesses it is in the same class (i.e. not derived) then you can access it and get the invocation list.

MulticastDelegate m = (MulticastDelegate)MyEvent;  

var list = m.GetInvocationList();  

foreach(Delegate d in list)  
{  
    // look at the delegate
}  

For the case where you want to access the invocation list of an event defined in a class whose code you can't modify...

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47
-3

You can try with this code - Just casting

var result = (EventHandler)control.Click;
Console.WriteLine(result.Method.Name);
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51