0

Add handler

if(!ClickHandled)
    this.Click += (s, e) =>{ }

Remove handler:

if(ClickHandled)
    this.Click -= (s, e) =>{ }

Is there a way to know if there is already an event handler attached to the control (and possibly get the list of them for example get the list of event handlers for click event)?

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • If you have access to source of that class which exposes event then you can use delegate to get all the event handlers....but if you want do this with built in control...then reflection as mentioned in above link can be used – Viru Feb 26 '16 at 09:47
  • 3
    Might be an XYproblem. Why do you need to know number of handlers? Event is a way to signal about something and it shouldn't matter if there is subscriber or not. – Sinatr Feb 26 '16 at 09:49
  • well I have access to source of the class – Ashkan Mobayen Khiabani Feb 26 '16 at 09:50
  • @Sinatr I have a class that inherits from `FileSystemWatcher` class and have `WatchForChanges` and `WatchForCreations` peoperties that take booean value. I want add handlers when the value set to true and remove it when they are set to false – Ashkan Mobayen Khiabani Feb 26 '16 at 10:01
  • 1
    If that's all you want to do, you don't need to see if there are any handlers currently attached. You store the previous value of the properties, and if the value has *changed*, you hook up or remove the handler. Very simple. – Cody Gray - on strike Feb 26 '16 at 10:13
  • I can offer you another approach (if I understood your goal correctly): define 2 events `FileChanged` and `FileCreated`. The user of your class can have a choice to which event subscribe. Next thing is you don't remove/add subscribers yourself, instead you simply fire or don't fire corresponding event. E.g. you can have property `IsMonitoringSuspended` and if its set to `true`, then simply don't rise any event (note, in such scenario you want to store changes, perhaps using queue and once property is set to `false` *replay* notifications to subscribers).No need to know if subscribers exists – Sinatr Feb 26 '16 at 10:29
  • @Sinatr like this? `watcher.Created += (s, e) =>{ if(!WatchChanges) return; .... }` – Ashkan Mobayen Khiabani Feb 26 '16 at 10:44

3 Answers3

1

You can implement a class inherited from EventHandler. For this class you can implement any additional behavior you want. For instance, you can create a collection which will hold object-event maps and you can implement a method which searches for a given pair or pattern.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

you can do this assuming you have access to source of class. Beware this way you are giving away the control of when to call all delegates to client of your class which is not a good idea. If you are not looking for the list of eventhandler but just want to know if event is subscribed.Probably you can use the other method which only tells whether click event is subscribed by anyone.

 class MyButton
    {
     delegate void ClickHandler(object o ,EventArgs e);
     public event ClickHandler Click;
     ......

      public List<ClickHandler> ClickHandlerList
      {
         get
          {
              return ClickHandler.GetInovationList().Cast<ClickHandler>().ToList();
          }
       }

      public bool IsClickEventSubcribed
      {
         get
          {
              return ClickHandler.GetInovationList().Cast<ClickHandler>().Any();
          }
       }
    }
Viru
  • 2,228
  • 2
  • 17
  • 28
-1

If the purpose of this is to stop sending signals to event listeners, wouldn't it be easier just wrap the sending by the check?

if (NotifyingEnabled)
{
    SomeEvent.Raise(this);
}
Divisadero
  • 895
  • 5
  • 18