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.
-
http://stackoverflow.com/questions/2952557/is-there-a-way-to-know-in-vb-net-if-a-handler-has-been-registered-for-an-event/2953318#2953318 – Stefan Jul 30 '11 at 12:11
-
1The entire point of the *Event* keyword is to make that impossible. Winforms has an extra layer of protection against this so it is next to impossible even with reflection. See http://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anothe/293031#293031 – Hans Passant Jul 30 '11 at 13:15
1 Answers
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.

- 4,868
- 2
- 22
- 32