Home All Groups Group Topic Archive Search About

Getting the handler of an event with Reflection

Author
9 Aug 2006 5:17 PM
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

Author
9 Aug 2006 7:38 PM
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.

Show quote
"Charles Bazi" <cb***@noos.fr> wrote in message
news:uxGwXc9uGHA.4296@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
Author
9 Aug 2006 8:58 PM
David Browne
Show quote
"Charles Bazi" <cb***@noos.fr> wrote in message
news:uxGwXc9uGHA.4296@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/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.");
    }
  }
}
Author
10 Aug 2006 8:14 AM
Charles Bazi
David Browne wrote:
Show quote
> "Charles Bazi" <cb***@noos.fr> wrote in message
> news:uxGwXc9uGHA.4296@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/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

}
Author
10 Aug 2006 2:56 PM
Carl Daniel [VC++ MVP]
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
Author
10 Aug 2006 3:06 PM
Charles Bazi
OK, thank you.

Carl Daniel [VC++ MVP] wrote:
Show quote
> 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
>
>

AddThis Social Bookmark Button