How to check the assigned event handler or not

my previous question is how to clear event descriptors in C #

Now I need to know how to verify that some event handler has already been processed?

+3
source share
4 answers

If the event is in the same class where you do the check, you can compare with null. But if this is not the case, you should ask yourself why you care about the inner workings of any class. I mean that the work of a class that contains an event takes care of its subscribers, not vice versa. But if you really want this information event containing class can expose a property to the outside world - for example HasEventHandlers.

+1
source

If I misunderstood the question, a simple check for null is enough. In any case, you always need to check for zero in the event handler before invoking any event handlers.

0
source

btnSubmit :

(((System.Web.UI.Control)(btnSubmit)).Events.head.handler).Method
0

, , , .

:

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 event that you have multicast delegates, you may encounter odd behavior.

0
source

Source: https://habr.com/ru/post/1750908/


All Articles