That's not so easy. Events declared in a class only expose add
and remove
to users of that class. You can access the invocation list of the event only from inside the class.
So one way you may achieve this is to inherit your own MyButton
class like this:
public class MyButton : Button
{
private event EventHandler MyClick;
public new event EventHandler Click
{
add
{
MyClick += value;
base.Click += value;
}
remove
{
MyClick -= value;
base.Click -= value;
}
}
public Delegate[] GetClickHandlers()
{
return MyClick?.GetInvocationList();
}
}
So I create a new Click
event that wraps the base.Click
event and stores the invocation list in an extra event.
You can than get the names of the associated handlers like this:
Console.WriteLine(
string.Join(Environment.NewLine,
button1.GetClickHandlers().Select(h => h.Method.Name)));
But now that I see this code, you could probably store/remove the value
s passed to add
and remove
in a separate list instead of using MyClick
, but the idea is the same.
Your main problem is that you cannot call GetInvocationList()
on Button.Click
.