Getting the InvocationList for an event in a different class

  • Thread starter Thread starter Andrew Ducker
  • Start date Start date
A

Andrew Ducker

I have a class with a DataTable built into it. Various places
subscribe to the DataTable.RowChanging event. I want to grab a list of
all the subscribers.

Normally I'd use GetInvocationList, but that only seems to work on
events that are defined in the same class. Events declared inside a
different class (even one inside the current one) won't let you use
that syntax.

Anyone got any ideas?

Thanks,

Andrew Ducker
 
Dnia 21-03-2006 o 11:37:17 Andrew Ducker said:
I have a class with a DataTable built into it. Various places
subscribe to the DataTable.RowChanging event. I want to grab a list of
all the subscribers.

Normally I'd use GetInvocationList, but that only seems to work on
events that are defined in the same class. Events declared inside a
different class (even one inside the current one) won't let you use
that syntax.

Anyone got any ideas?

[PD] If you really know what you are doing you can use Reflector to check
how looks add method for this event. If it's "standard" event without
custom add method C# compiler generates hidden field and add and remove
methods operating on it, so you can acces this hidden delegate using
reflection and get the invocation list. In other case you must analyze the
custom code to see where the added delegates are stored and also acces it
via reflection.
 
Hi,

Normally I'd use GetInvocationList, but that only seems to work on
events that are defined in the same class. Events declared inside a
different class (even one inside the current one) won't let you use
that syntax.

Anyone got any ideas?

A possible workaround is to define the event in your outer class, all the
consumers hook to this event, then only you subscribe to the DataTable'e
event. In this case you can do as you usually do.
 
Andrew,

You cannot get the invocation list of event in other class. Events are
metadata objects only and they are set of 2 or 3 methods - add, remove and
invoke. How the events are implemented internaly is up to the implementing
classl it might use private field, collection of event handlers
(System.Windows.Forms controls for example) or maybe some other weird
method.

Keep in mind that invication list belongs to the delegate not the event
itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top