It’s possible via C# code get an list of methods/delegates that was set to Control? let me explain better..
For example.
Assuming some definitions like this:
foo.Click += (a, b) => { ... }
//..
foo.Click += (A,B) => { ... }
And a megic method:
var baa = foo.GetEvents("Click");
Returns
baa[0] points to (a, b) => { ... }
baa[1] points to (A,B) => { ... }
My scenery: I make and add dynamically event to some controls inside a loop. I want depending to a boolean value a event of control of index one,will removed by using control.Click -= baa[1] or something like this. I hope this is clear for your. Thanks in advance.
No, there’s no way to do that. The point of events is to hide the backing delegate field (well, strictly speaking, there might not be a backing field at all), so that you can only subscribe to or unsubscribe from the event.
The reason for this is that a subscriber (aka “observer” as in the Observer pattern) is not supposed to know about other subscribers, let alone unsubscribe them. What would you say if your neighbor canceled your subscription to your favorite magazine ? 😉
EDIT: actually, there is a way to do that, but it’s ugly and unreliable. Assuming you know how the class stores its event handlers (usually a delegate field, but in Windows Forms it’s different), you can use reflection on private members to retrieve the event handlers.