473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 2523
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Flack | last post by:
Hello, Is it possible to find out how many methods are listening to a certain event? For example, if a number of methods subscribed to a controls DragDrop event using +=, can I find out how many...
1
by: Flack | last post by:
Hello, Is it possible to find out how many methods are listening to a certain event? For example, if a number of methods subscribed to a controls DragDrop event using +=, can I find out how many...
4
by: Tedb | last post by:
Is there any reason why you can't reuse a delegate object instance versus creating a new one each time? For example in the following scenario I have a DataPoints object with an array of DataPoint...
0
by: U S Contractors Offering Service A Non-profit | last post by:
This Sunday the 26th 2006 there will be Music @ Tue Nov Inbox Reply Craig Somerford to me show details 9:54 pm (26 minutes ago) #1St "CLICK" HeAt frOm A blanket...
9
by: stevewy | last post by:
I am trying to write a function that will test all the checkboxes in a particular group of a form (it's a questionnaire), see whether more than three of them are ticked, and display a message if...
2
by: erbilkonuk | last post by:
Hi, I am very new to .NET Remoting and I try to run a simple program to subscribe to an event raised by Remoting Class. The Remoting Server initiates an instance of Remoting Class as Singleton /...
3
by: Cartoper | last post by:
My application appears to have a recourse leak. When the user starts a background process, the handle count in Process Explorer (PE) goes up by about 10, sometime 1 or 2 more, sometimes 1 or 2...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
tlhintoq
by: tlhintoq | last post by:
I'm pretty sure this is language independent and is going to be the same whether it's VC or C# - but my project is C# WIndows Forms just in case. Does anyone have a good handle on the sequence of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.