Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has an event handler already been added?

Tags:

c#

.net

asp.net

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.

like image 591
CodeRedick Avatar asked Sep 25 '08 23:09

CodeRedick


People also ask

Are event handlers and event listeners the same?

Note: Event handlers are sometimes called event listeners — they are pretty much interchangeable for our purposes, although strictly speaking, they work together. The listener listens out for the event happening, and the handler is the code that is run in response to it happening.


2 Answers

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 :)

like image 67
alf Avatar answered Oct 07 '22 12:10

alf


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.

like image 22
Blair Conrad Avatar answered Oct 07 '22 11:10

Blair Conrad