You cannot do that from outside the object, either in C# or in VB. An event is basically two accessors: add
and remove
. This is immediately visible if you hand-code it:
public event EventHandler Click
{
add { ... }
remove { ... }
}
These become add_Click(EventHandler)
and remove_Click(EventHandler)
methods. Even if you use default event implementation:
public event EventHandler Click;
it's still no different, only the accessors are generated for you with default implementation, which uses a private multicast delegate field with the same name as event to store those handlers.
This means two things:
For clients of the class, the only two things they can do about an event is add
or remove
handlers, since only accessors are exposed. There's no accessor to list currently registered handlers
Even if you use default implementation for events, which provides a field, that field is still private, so you can't access it except from inside a method of the same class. You can use reflection if you have code permission to do so, but see #1 for why it's not a general solution.
This is actually done on purpose. The reason is this: the object may be shared, and other code may have registered its handlers for its events. If you get access to list of handlers, you'd be able to call them yourself, potentially breaking the contract, and accessing private methods in a manner the owner of the class did not intend.
If you want this kind of thing, it needs to be done in the class providing the event - either write your own add
and remove
to check for dupes, or expose the private field via a property.