Has an event handler already been added?

Is there a way to tell if an event handler has been added to an object? I’m serializing a list of objects into/out of session state so we can use SQL based session state… When an object in the list has a property changed it needs to be flagged, which the event handler took care of properly before. However now when the objects are deserialized it isn’t getting the event handler.

In an fit of mild annoyance, I just added the event handler to the Get property that accesses the object. It’s getting called now which is great, except that it’s getting called like 5 times so I think the handler just keeps getting added every time the object is accessed.

It’s really safe enough to just ignore, but I’d rather make it that much cleaner by checking to see if the handler has already been added so I only do so once.

Is that possible?

EDIT: I don’t necessarily have full control of what event handlers are added, so just checking for null isn’t good enough.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I recently came to a similar situation where I needed to register a handler for an event only once. I found that you can safely unregister first, and then register again, even if the handler is not registered at all:

myClass.MyEvent -= MyHandler;
myClass.MyEvent += MyHandler;

Note that doing this every time you register your handler will ensure that your handler is registered only once.
Sounds like a pretty good practice to me 🙂

Method 2

From outside the defining class, as @Telos mentions, you can only use EventHandler on the left-hand side of a += or a -=. So, if you have the ability to modify the defining class, you could provide a method to perform the check by checking if the event handler is null – if so, then no event handler has been added. If not, then maybe and you can loop through the values in
Delegate.GetInvocationList. If one is equal to the delegate that you want to add as event handler, then you know it’s there.

public bool IsEventHandlerRegistered(Delegate prospectiveHandler)
    if ( this.EventHandler != null )
        foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
            if ( existingHandler == prospectiveHandler )
                return true;
    return false;

And this could easily be modified to become “add the handler if it’s not there”. If you don’t have access to the innards of the class that’s exposing the event, you may need to explore -= and +=, as suggested by @Lou Franco.

However, you may be better off reexamining the way you’re commissioning and decommissioning these objects, to see if you can’t find a way to track this information yourself.

Method 3

If this is the only handler, you can check to see if the event is null, if it isn’t, the handler has been added.

I think you can safely call -= on the event with your handler even if it’s not added (if not, you could catch it) — to make sure it isn’t in there before adding.

Method 4

This example shows how to use the method GetInvocationList() to retrieve delegates to all the handlers that have been added. If you are looking to see if a specific handler (function) has been added then you can use array.

public class MyClass
  event Action MyEvent;


MyClass myClass = new MyClass();
myClass.MyEvent += SomeFunction;


Action[] handlers = myClass.MyEvent.GetInvocationList(); //this will be an array of 1 in this example

Console.WriteLine(handlers[0].Method.Name);//prints the name of the method

You can examine various properties on the Method property of the delegate to see if a specific function has been added.

If you are looking to see if there is just one attached, you can just test for null.

Method 5

If I understand your problem correctly you may have bigger issues. You said that other objects may subscribe to these events. When the object is serialized and deserialized the other objects (the ones that you don’t have control of) will lose their event handlers.

If you’re not worried about that then keeping a reference to your event handler should be good enough. If you are worried about the side-effects of other objects losing their event handlers, then you may want to rethink your caching strategy.

Method 6

The only way that worked for me is creating a Boolean variable that I set to true when I add the event. Then I ask: If the variable is false, I add the event.

bool alreadyAdded = false;

This variable can be global.

    myClass.MyEvent += MyHandler;
    alreadyAdded = true;

Method 7

i agree with alf’s answer,but little modification to it is,,
to use,

                control_name.Click -= event_Click;
                main_browser.Document.Click += Document_Click;
            catch(Exception exce)
                main_browser.Document.Click += Document_Click;

Method 8

EventHandler.GetInvocationList().Length > 0

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x