Getting the handler of an event with Reflection

C

Charles Bazi

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
 
K

Kevin Spencer

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.
 
D

David Browne

Charles Bazi said:
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/library/system.delegate.getinvocationlist.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.");
}
}
}
 
C

Charles Bazi

David said:
Charles Bazi said:
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/library/system.delegate.getinvocationlist.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

}
 
C

Carl Daniel [VC++ MVP]

Charles said:
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
 
C

Charles Bazi

OK, thank you.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top