måndag 13 juni 2011

VB.NET: Finding registered event handlers

I was wondering if there was any way in VB to see if there are any registered handlers to a specific event. I knew it could easily be done in C#, but did not know of an equivalent for VB. And after some googling, I found it. But of course, with some "quirks"..

It seems that you can only check for registered event handlers on custom events, and only by the obscure:

...
Public Event MyEvent As Action(Of T)
...
If MyEventEvent Is Nothing Then
...
So, it has to be a custom event, and you have to append 'Event' to the name of your event.. And Intellisense will not help you as it does not even allow you to type the name of your event if you have not started the line with RaiseEvent...

I can live with the fact that I have to append 'Event' to the name of my event, but why does it have to be a custom event? Why does it not work with other events such as Button.Click?

I suspect that this is because of that the field MyEventEvent might be private or protected though. So maybe you could do it via reflection. But come on.

Inga kommentarer:

Skicka en kommentar