472,122 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

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

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
em**************@fastmail.fm
Nov 15 '05 #1
1 2462
100
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

"emma middlebrook" <em**************@fastmail.fm> wrote in message
news:e2**************************@posting.google.c om...
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
em**************@fastmail.fm

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tedb | last post: by
reply views Thread by U S Contractors Offering Service A Non-profit | last post: by
9 posts views Thread by stevewy | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.