|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Getting the handler of an event with ReflectionHi,
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 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? -- Show quoteHTH, Kevin Spencer Microsoft MVP Chicken Salad Surgery Accept the Unexpected. "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
Show quote
"Charles Bazi" <cb***@noos.fr> wrote in message Delegate.GetInvocationList Methodnews: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. > 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."); } } } David Browne wrote:
Show quote > "Charles Bazi" <cb***@noos.fr> wrote in message Hi, this is interesting, but I'm trying to que get the InvocationList > 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."); > } > } > } > > 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 } Charles Bazi wrote:
> Hi, this is interesting, but I'm trying to que get the InvocationList In general, this cannot be done. From the outside, an event exposed by a > from another class, and I don't know how to get a reference to the > Event. 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 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 > > |
|||||||||||||||||||||||