473,404 Members | 2,174 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,404 software developers and data experts.

Events and delegates

NOTE: I'm new to c#, but not to programming.

For reasons unimportant to the discussion below, I need to be able
to dynamically, at run time, discover the set of all objects listening to
another object's events.

Given some jRandomType, I've figured out how to get the Events
themselves via reflection and GetEvents/EventInfo. This will give me
a list of the Events that jRandomType wants to raise -- but that's only
half the battle.

Now, the part that's got me stumped: Once I have a list of Events
that a Type raises, how do I get from there to iterating through the
subscribers that are listening to the Events on some instance of
jRandomType?

I understand that the Events are defined by the *class*, and the
listeners are registered on a particular *instance* of that class -- e.g.
a given class may raise a single Event, but three instances of that
class may have three different listeners registered. That's not a big
conceptual problem for me. But -- given that I can find the Events
raised via reflection, how do I find out what's subscribed as a
listener on a particular instance?

Is this a case of "you can't get there from here" ? Or, is there an
easy way I'm not finding in the docs?

Thanks for your help.

Nov 16 '05 #1
6 1641
Sgt. Sausage <no****@nowhere.com> wrote:
NOTE: I'm new to c#, but not to programming.

For reasons unimportant to the discussion below, I need to be able
to dynamically, at run time, discover the set of all objects listening to
another object's events.

Given some jRandomType, I've figured out how to get the Events
themselves via reflection and GetEvents/EventInfo. This will give me
a list of the Events that jRandomType wants to raise -- but that's only
half the battle.

Now, the part that's got me stumped: Once I have a list of Events
that a Type raises, how do I get from there to iterating through the
subscribers that are listening to the Events on some instance of
jRandomType?

I understand that the Events are defined by the *class*, and the
listeners are registered on a particular *instance* of that class -- e.g.
a given class may raise a single Event, but three instances of that
class may have three different listeners registered. That's not a big
conceptual problem for me. But -- given that I can find the Events
raised via reflection, how do I find out what's subscribed as a
listener on a particular instance?

Is this a case of "you can't get there from here" ? Or, is there an
easy way I'm not finding in the docs?


You can't in all cases. In some cases it's relatively straightforward.
If the event has been defined in C# as:

public event Foo;

then the compiler will have generated a field (also called "Foo" with
the MS compiler, but it's not guaranteed) of the appropriate delegate
type.

There's no guarantee that it's been done that way. For instance, you
could write a perverse event:

public event Amnesiac
{
add {}
remove {}
}

which basically doesn't act properly at all - it forgets whatever is
added to it!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
mdb
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
You can't in all cases. In some cases it's relatively straightforward.
If the event has been defined in C# as:

public event Foo;


Assuming that the event is formed in a normal manner, though, you can
access the Method and Target parameters of the event delegates...

public event Event_Handler evt;
Delegate[] delArray = evt.GetInvocationList();
for(int i=0; i<delArray.Length; i++)
{
Delegate ev = delArray[i];
Trace.WriteLine(ev.Method);
Trace.WriteLine(ev.Target);
}

-mdb
Nov 16 '05 #3

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Sgt. Sausage <no****@nowhere.com> wrote:


[snip]
Is this a case of "you can't get there from here" ? Or, is there an
easy way I'm not finding in the docs?


You can't in all cases. In some cases it's relatively straightforward.
If the event has been defined in C# as:

public event Foo;

then the compiler will have generated a field (also called "Foo" with
the MS compiler, but it's not guaranteed) of the appropriate delegate
type.


That's not really any help now is it? <grin>

[snip]

So, in this case, how do I grab it and iterate through the listeners?


Nov 16 '05 #4

"mdb" <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in message
news:Xn****************************@207.46.248.16. ..
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
You can't in all cases. In some cases it's relatively straightforward.
If the event has been defined in C# as:

public event Foo;


Assuming that the event is formed in a normal manner, though, you can
access the Method and Target parameters of the event delegates...

public event Event_Handler evt;
Delegate[] delArray = evt.GetInvocationList();
for(int i=0; i<delArray.Length; i++)
{
Delegate ev = delArray[i];
Trace.WriteLine(ev.Method);
Trace.WriteLine(ev.Target);
}


Thanks, but that doesn't get it either.

Given evt.GetInvocationList() -- how does one do this
dynamically, at runtime. You knew, at compile time, that
it's "evt". I don't know this.

I know there's an event named "evt" -- I got that via the
Reflection mentioned in my original post. How do I,
at run time, do something like:

this.("evt").GetInvocationList

or, more to my question:

EventInfo[] events = this.GetType().GetEvents( BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic );

for (int i = 0; i < events.Length; i++)
{

// This is the line (below)

this.(events[i].Name).GetInvocationList //wrong!!

// how do I get the InvocationList, knowing that I've got the name
// of the event in events[i].Name ???

}




Nov 16 '05 #5

"Sgt. Sausage" <no****@nowhere.com> wrote in message
news:3v*******************@fe37.usenetserver.com.. .

"mdb" <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in message
news:Xn****************************@207.46.248.16. ..
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
You can't in all cases. In some cases it's relatively straightforward.
If the event has been defined in C# as:

public event Foo;
Assuming that the event is formed in a normal manner, though, you can
access the Method and Target parameters of the event delegates...

public event Event_Handler evt;
Delegate[] delArray = evt.GetInvocationList();
for(int i=0; i<delArray.Length; i++)
{
Delegate ev = delArray[i];
Trace.WriteLine(ev.Method);
Trace.WriteLine(ev.Target);
}


Thanks, but that doesn't get it either.

Given evt.GetInvocationList() -- how does one do this
dynamically, at runtime. You knew, at compile time, that
it's "evt". I don't know this.

I know there's an event named "evt" -- I got that via the
Reflection mentioned in my original post. How do I,
at run time, do something like:

this.("evt").GetInvocationList

or, more to my question:

EventInfo[] events = this.GetType().GetEvents( BindingFlags.Instance |
BindingFlags.Public |

BindingFlags.NonPublic );
for (int i = 0; i < events.Length; i++)
{

// This is the line (below)

this.(events[i].Name).GetInvocationList //wrong!!

// how do I get the InvocationList, knowing that I've got the name
// of the event in events[i].Name ???

}


Hate to reply to my own post, but on another forum I found this in the
archives:

Recently I wanted to test whether we were successfully releasing
all our event subscriptions when an object was disposed. I thought
this would be a simple task, use Type.GetEvents() to get the events
and then use the resulting EventInfo's to get a list of subscribers. Not
quite so simple, while EventInfo has methods to add and remove events
, it has no invocation list. After a bit more work I discovered the
trick
is to ask for the events as fields and then cast the resulting object to
Delegate. Now I can invoke GetInvocationList(). Too bad MS didn't
include GetInvocationList() on the EventInfo. Problem solved: our unit
tests are starting to get vey good at catching subscriber leaks.

The thread is old, and long since dead.

If anyone knows the actual code to do as described above, I'd
like to see it.

Thanks.

Nov 16 '05 #6

"Sgt. Sausage" <no****@nowhere.com> wrote in message news:TGl1d.19891
Hate to reply to my own post, but on another forum I found this in the
archives:

Recently I wanted to test whether we were successfully releasing
all our event subscriptions when an object was disposed. I thought
this would be a simple task, use Type.GetEvents() to get the events
and then use the resulting EventInfo's to get a list of subscribers. Not quite so simple, while EventInfo has methods to add and remove events
, it has no invocation list. After a bit more work I discovered the
trick
is to ask for the events as fields and then cast the resulting object to Delegate. Now I can invoke GetInvocationList(). Too bad MS didn't
include GetInvocationList() on the EventInfo. Problem solved: our unit
tests are starting to get vey good at catching subscriber leaks.

The thread is old, and long since dead.

If anyone knows the actual code to do as described above, I'd
like to see it.

Thanks.


Again with the replying to my own post -- figured it out.

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marty McDonald | last post by:
It is still unclear to me why we would use events when delegates seem to do just fine. People say that events make it so the publisher doesn't need to know about the listeners. What does that...
7
by: Rakesh Rajan | last post by:
Hi, I find that when i define a delegate, it gets derived from MulticastDelegate, which provides all funtionality that events would provide (like registering new handlers etc.). Then, apart from...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
3
by: Chris | last post by:
Hi, what is the difference between using events and delegates (apart from the syntax) ? have a look at following (working) programs please (you can just copy/paste and build it) : First...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.