Check if a specific event handler method already attached
Asked Answered
P

4

17

Related to this question, Check if an event already exists

but the difference is I just want to know if a particular method is attached to the event. So there may be other methods attached, but I just want to know if a particular one exists.

My environment is C# in dotnet 4.0.

E.g.

Event += MyMethod1;
Event += MyMethod2;

// Some code
if (MyMethod1IsAttachedToEvent())
{
    // Achieved goal
}

Is this possible?

Paediatrician answered 6/3, 2013 at 3:56 Comment(3)
possible duplicate : #2697747Misbehave
Ah yes... could use that approach thanks (the first answer to that question)Paediatrician
or indeed duplicate of #137475Grooved
M
24

No. You cannot.

The event keyword was explicitly invented to prevent you from doing what you want to do. It makes the delegate object for the event inaccessible so nobody can mess with the events handlers.

Source : How to dermine if an event is already subscribed

Misbehave answered 6/3, 2013 at 3:59 Comment(1)
Thanks yes that was my impression. Wondered if there was a way around it.Paediatrician
A
6

Late answer here. I believe Parimal Raj answer is correct, as I could not find a way to directly access the events. However, here are two methods I created to get around this:

  1. Delete before adding. If the method isn't there, I did not receive an error trying to delete the nonexistant method. This way you can insure the invocation list calls method1 only once.

    Event -= MyMethod1;
    Event += MyMethod1;
    
  2. The objects you are adding an event to may have a .Tag property. You can use the Tag to store info about the methods you already added. Here I only have one method, so I just need to check if Tag is null. But more complicated scenarios can be handled this way:

    if(control.Tag == null)
    {
         //ony added once, when tag is null
         control.TextChanged += new EventHandler(validate); 
         control.Tag = new ControlTag();
    }
    
Agc answered 5/9, 2020 at 21:24 Comment(0)
L
5
Event.GetInvocationList().Any(x => x.Method.Name.Equals("yourmethodname"));
Legalize answered 6/3, 2013 at 3:59 Comment(0)
C
5
foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
{
    if ( existingHandler == prospectiveHandler )
    {
          return true;
    }
}

loop through the delegates using the GetInvocationList method.

Celestecelestia answered 6/3, 2013 at 4:0 Comment(4)
Was under the impression, as per other posts, that GetInvocationList isn't always accessible...Paediatrician
It is a part the framework. Completely acceptable. What isn't acceptable is sloppy code that incorrectly implements it and leaves objects laying around willy nilly. Always dispose your classes. For what you asked it is a perfectly acceptable practice.Celestecelestia
"It is a part the framework. Completely acceptable." What I meant is if the event is from a 3rd party component you may not have access to call GetInvocationList(). Different to acceptability.Paediatrician
The .GetInvocationList() is available only iin the class where the event is being declared.. how do I use it in another class (for example where I am using this); I need it in a Monitor Class to identify how many event handlers are associated with an event.. how do I?Papeterie

© 2022 - 2024 — McMap. All rights reserved.