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

Getting the handler of an event with Reflection

Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to
see if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.

Any idea ?

TIA
Aug 9 '06 #1
5 3585
Adding an Event Handler is process. That is, the Event Handler definition is
part of the class, but the Event Handler is not assigned to the Event until
the process is running (hence, "AddHandler" or the C# equivalent).
Therefore, you cannot discover whether an event has a handler via
Reflection. The only way to know if an Event has a handler is to check the
Event object itself. If it is null, it has no Handler assigned to it.

However, I get the feeling that you're talking about something you haven't
actually mentioned. What is the requirement you are trying to fulfill?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Accept the Unexpected.

"Charles Bazi" <cb***@noos.frwrote in message
news:ux**************@TK2MSFTNGP06.phx.gbl...
Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to see
if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.

Any idea ?

TIA

Aug 9 '06 #2

"Charles Bazi" <cb***@noos.frwrote in message
news:ux**************@TK2MSFTNGP06.phx.gbl...
Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to see
if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.
Delegate.GetInvocationList Method
http://msdn2.microsoft.com/en-us/lib...ationlist.aspx

EG
using System;
using System.Collections.Generic;
using System.Reflection;

namespace csTest
{
class Program
{

delegate void MyEventDelegate(object Sender, EventArgs args);
static event MyEventDelegate MyEvent;

public static void Main(string[] args)
{
MyEvent += new MyEventDelegate(Program_MyEvent);
foreach (Delegate listener in MyEvent.GetInvocationList())
{
Console.WriteLine(listener.Method.Name);
}
}

static void Program_MyEvent(object Sender, EventArgs args)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
Aug 9 '06 #3


David Browne wrote:
"Charles Bazi" <cb***@noos.frwrote in message
news:ux**************@TK2MSFTNGP06.phx.gbl...
>Hi,

I have a base UserControl named UserControlEx. I have created an event,
and a dummy handler.

Then I create several UCs with UserControlEx base. Some of those have a
handler for my custom event, others, no...

I manage to have the list of events with EventInfo, but I'm unable to see
if some event has a handler associated...

I've been googling and testing all the afternoon with no luck.

Delegate.GetInvocationList Method
http://msdn2.microsoft.com/en-us/lib...ationlist.aspx

EG
using System;
using System.Collections.Generic;
using System.Reflection;

namespace csTest
{
class Program
{

delegate void MyEventDelegate(object Sender, EventArgs args);
static event MyEventDelegate MyEvent;

public static void Main(string[] args)
{
MyEvent += new MyEventDelegate(Program_MyEvent);
foreach (Delegate listener in MyEvent.GetInvocationList())
{
Console.WriteLine(listener.Method.Name);
}
}

static void Program_MyEvent(object Sender, EventArgs args)
{
throw new Exception("The method or operation is not implemented.");
}
}
}

Hi, this is interesting, but I'm trying to que get the InvocationList
from another class, and I don't know how to get a reference to the Event.

I have classA.cs with these code:

public event EventHandler PrintEventHandler;

And then, classB.cs, where I need to get the GetInvocationList

public void Adapting (Type pType, Control pControl) {

System.Reflection.EventInfo[] events = pControl.GetType().GetEvents();

// And now, I have access to PrintEventHandler from an EventInfo, but
I'm unable to get an object of type PrintEventHandler to get the
GetInvocationList method...

TIA

}
Aug 10 '06 #4
Charles Bazi wrote:
Hi, this is interesting, but I'm trying to que get the InvocationList
from another class, and I don't know how to get a reference to the
Event.
In general, this cannot be done. From the outside, an event exposed by a
class is a pair of functions: EventName_Add and EventName_Remove. Unless
you're going to get into decompiling and interpreting the IL behind those
functions, there's simply no way to locate the delegate whose invocation
list you'd need to enumerate. Worse, there's no requirement that there even
be a delegate. The event producer may actually store a the callback
information in a structure other than a delegate (for example, it might
actually pass the delegates on to another object if it's simply forwarding
an event from a contained object).

-cd
Aug 10 '06 #5
OK, thank you.

Carl Daniel [VC++ MVP] wrote:
Charles Bazi wrote:
>Hi, this is interesting, but I'm trying to que get the InvocationList
from another class, and I don't know how to get a reference to the
Event.

In general, this cannot be done. From the outside, an event exposed by a
class is a pair of functions: EventName_Add and EventName_Remove. Unless
you're going to get into decompiling and interpreting the IL behind those
functions, there's simply no way to locate the delegate whose invocation
list you'd need to enumerate. Worse, there's no requirement that there even
be a delegate. The event producer may actually store a the callback
information in a structure other than a delegate (for example, it might
actually pass the delegates on to another object if it's simply forwarding
an event from a contained object).

-cd

Aug 10 '06 #6

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

Similar topics

1
by: Cindy Liu | last post by:
Hi Everyone, I can use .Net Reflection to know there is an event defined in a class. But I don't know how to get the signature of that event handler? Any idea? Thanks in advance!!! Cindy
6
by: kurotsuke | last post by:
Hi, I'm trying to use the GetKeyName API to get the name of certain keys (in the system language). For example the names of SHIFT and RETURN keys as well as the names of same special characters....
1
by: Ivan | last post by:
subject says it all. thanks in advance. Ivan
3
by: Franco, Gustavo | last post by:
How can I remove all event handler from one event without do -=? I won't explain why because is too long, and that the only option left I have right now. I have one event declared for: ...
0
by: Stefan Rosenthal | last post by:
Hi all, I've an OL-Style App loading MDIChild-Windows (Panes) on demand dynamicaly at runtime. Loading the Forms works fine but establishing an EventHandler (to receive information from the...
7
by: Peter Row | last post by:
Hi, I've started work on my own control some parts of which use standard controls, others I need to draw on my controls surface to get the display output I require, however.... I seem to be...
0
by: JimN1 | last post by:
Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows. This is a continuation of my previous table element update question. I am now getting the above...
3
by: wolverine | last post by:
Hi, I am injecting a javascript code into html pages and attaching some dom events to some elements via attachEvent (IE only). I just want to know that is there any chance by which my event...
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.