Reputation: 10981
my previos question is how to clear event handles in c#
Now i need to know that how to check any event handler already assigned?
Upvotes: 3
Views: 4246
Reputation:
An old solution presented by Jon Skeet where you explicitly implemented event handling would be the best way to address this.
Here is how:
private EventHandler m_myEvent;
public event EventHandler OnEvent
{
add
{
// First try to remove the handler, then re-add it
m_myEvent -= value;
m_myEvent += value;
}
remove
{
m_myEvent -= value;
}
}
In the unlikely scenario that you have multicast delegates, you could experience odd behaviors.
Upvotes: 0
Reputation: 47
In quick watch window I found the btnSubmit
click handler with the following expression:
(((System.Web.UI.Control)(btnSubmit)).Events.head.handler).Method
Upvotes: 0
Reputation: 47373
If the event is in the same class where you will do the check, you can compare to null
. But if this is not the case, you should ask yourself why do you care about the inside workings of a class. I mean it is the job of the class which contains the event to care about its subscribers not the opposite. But if you really want this information, the event containing class can expose a property for the outside world - like HasEventHandlers
.
Upvotes: 1
Reputation: 7201
Unless I misunderstand the question, a simple check for null should be sufficient. You always need to check for a null in the event handler anyway before calling any event handlers.
Upvotes: 0