Finding out which objects have subscribed to a particular event ...

  • Thread starter Thread starter emma middlebrook
  • Start date Start date
E

emma middlebrook

Hi

I want to find out what objects are due to receive an event i.e. those
that have added themselves as an event handler via +=. Yes, it's a
little pointless perhaps (or can anyone give some good uses for
this?!!).

How do I do this for an event on a class I implement? Also, how may I
do this for an event in the .Net framework e.g. a control?

This is for interest and knowledge so replies similar to "why do you
want to do this - it's not good practice because ..." are not
acceptable! :-)

Cheers

Emma Middlebrook
(e-mail address removed)
 
Hi Emma,

The answer of your question is: You might be able to find out the objects.
Basically events are not more then two accessor methods called *add* and
*remove* which are similar to the accessors for properties *get* and *set*.
The event MyEvent looks like

public event MyDelegate MyEvent
{
add
{
//Keep the reference to the delegate along with the references
already added
}
remove
{
//remove reference to the delegate
}

}

How the class will keep the references to delegates and call them when the
event should be fired is up to the implementer of the class.

Usually classes, which does not provide a lot of events use event keyword to
define an event:

<acces modifier> event MyDelegate MyEvent

The above line will be compiled by the c# compiler to something close to the
following lines:

private MyDelegate myEvent;

<acces modifier> event MyDelegate MyEvent
{
add
{
myEvent Delegate.Combine(value, myEvent)
}
remove
{
myEvent = Delegate.Remove(myEvent, value)
}
}

So, you can see the Delegate reference, which holds the handlers list is
always private and no one from outside the class can access it in order to
traverse the list. Anyway inside the class list can be traversed.
This can be done in several ways. One of them is using
Delegate.GetInvocationList, which returns an array of delegates. Each
delegate's Target property can be read in order to get the reference to the
subscribed object. This Property will be null if a static method has been
used.

However, if a class exposes a lot of events and the implementer expects a
few of them to be used at a time defining each event with the *event*
keyword is going to be a waste of memory (for each event a reference to a
delegate has to be allocated). Control class is good example of this. In
this case the delegates are kept in some kind of collection (usually
hashtable) and the delegate is added to the collection in the add accessor
if it is not already there.

So, You cannot find out the objects that have added themselves as an event
handler via += unless within the class itself. How can you do it depends on
the realization you have chosen.

I cannot say whether it is good or bad practice. If you need it of course
you can do it for your classes. Anyway you cannot do this for the class,
which you have not written :)

I'm not talking about using reflection to access private fields. Anyway, in
most of the cases there is no way for you to know how the delegate chains
are kept.

HTH
B\rgds
100
 
Back
Top