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?

Community
  • 1
  • 1
Shiv
  • 1,274
  • 1
  • 19
  • 23
  • possible duplicate : http://stackoverflow.com/questions/2697247/how-to-determine-if-an-event-is-already-subscribed – Parimal Raj Mar 06 '13 at 04:00
  • Ah yes... could use that approach thanks (the first answer to that question) – Shiv Mar 06 '13 at 23:22
  • or indeed duplicate of http://stackoverflow.com/questions/136975/has-an-event-handler-already-been-added – hawbsl Nov 06 '13 at 17:28

4 Answers4

23

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

Community
  • 1
  • 1
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
5
Event.GetInvocationList().Any(x => x.Method.Name.Equals("yourmethodname"));
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
5
foreach ( Delegate existingHandler in this.EventHandler.GetInvocationList() )
{
    if ( existingHandler == prospectiveHandler )
    {
          return true;
    }
}

loop through the delegates using the GetInvocationList method.

Benjiman
  • 420
  • 2
  • 9
  • 1
    Was under the impression, as per other posts, that GetInvocationList isn't always accessible... – Shiv Mar 06 '13 at 23:29
  • 1
    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. – Benjiman Mar 06 '13 at 23:47
  • 1
    "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. – Shiv Mar 07 '13 at 22:42
  • 4
    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? – Avdhut Vaidya Aug 28 '16 at 09:20
5

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();
    }
    
NameSpace
  • 10,009
  • 3
  • 39
  • 40