开发者

Is there a handler for event X?

Is it possible to determine using VB.NET whether a certain event has any handlers attached to it? I don't own the event,开发者_运维知识库 in this particular case I want to know which of the items in a Windows.Forms.MenuStrip have their ToolstripMenuItem.Click event handled.


I don't think this is possible without creating your own eventing structure, or possibly using reflection to get at private compiler implemented members.

I just compiled a small snippet and ran it through Reflector.

Original code

       RemoveHandler d.CollectionChanged, AddressOf DestinationsChanged
       AddHandler d.CollectionChanged, AddressOf DestinationsChanged

What ended up in reflector

     Me.$STATIC$get_Destinations$200126C$d.remove_CollectionChanged(New NotifyCollectionChangedEventHandler(Me, DirectCast(Me.DestinationsChanged, IntPtr)))
     Me.$STATIC$get_Destinations$200126C$d.add_CollectionChanged(New NotifyCollectionChangedEventHandler(Me, DirectCast(Me.DestinationsChanged, IntPtr)))

Notice that .net appears to make use of some compiler generated observable collections to track the events.

You +might+ be able to use reflection to reach into the class and retrieve those internally defined collections, then enumerate their contents, but I've never tried it.

Another option might be to roll your own event handling structure for the event in question.

Check this article out for more on that.

http://www.codeproject.com/KB/cs/EventChain.aspx

To investigate further, I'd highly recommend grabbing a copy of Reflector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜