When using a Delegate, I can use Delegate.GetInvocationList() Method to retrieve the invocation list of the delegate at runtime.
Is there a way to access the subscriber list that has been associated with an event? I ran the code from this example (SO#1237001) and the immediate window allowed me to cast the SecondChange event to a System.MultiCastDelegate and then invoke the GetInvocationList method.
However, in my scenario I am working with a System.Windows.Forms.DataGridView and I would like to inspect the invocation list of the CellClick event at runtime. However, when I try any kind of cast on CellClick, I recieve the following error:
The event
‘System.Windows.Forms.DataGridView.CellClick’
can only appear on the left hand side
of += or -=
I can see there are clearly differences in the declarations of these events. In the Clock example the event is declared like this:
public event Func<DateTime, bool> SecondChange;
And in the DataGridView the event is declared like this:
[SRDescription("DataGridView_CellClickDescr"), SRCategory("CatMouse")]
public event DataGridViewCellEventHandler CellClick
{
add
{
base.Events.AddHandler(EVENT_DATAGRIDVIEWCELLCLICK, value);
}
remove
{
base.Events.RemoveHandler(EVENT_DATAGRIDVIEWCELLCLICK, value);
}
}
Why can one call GetInvocationList on the Clock example, but not on the DataGridView event? Is there any way for me to get the same type of information from the DataGridView event that GetInvocationList returns?
The whole point of an
eventit defines (only) anadd/removeAPI (like how a property definesget/set).The entire point of an event is that externally you can’t do this (access the subscribers). I’m guessing that in the “clock” example, the code that accesses the list is inside the type that declares the
event; that is fine: inside the type, you have full access to the backing implementation (often a delegate field).Externally, you should only care about your own handlers, that you already know about because you subscribed them. Attempts to fetch the backer exist, but it is brittle and not recommended. In this case, it uses an
EventHandlerList, for example.Why do you need this? It usually means that you are doing something wrong (sorry, but it does).