title: How to disable subscription to an event from many instances of one type and allow only one?
body: <p>I have Windows Forms application with one main form (derived from base <code>Form</code>). Other modal forms that could be opened there are derived from my class <code>ManagedForm</code>, which is also derived from <code>Form</code>.<br />
   Also I have a static notifier service which fires some events like this:</p>
  
  <pre><code>    public static class NotifierService
      {
          public delegate void NotifierServiceEventHandler(object sender, NotifierServiceEventArgs e);
  
          private static readonly object Locker = new object();
          private static NotifierServiceEventHandler _notifierServiceEventHandler;
  
          #region Events
  
          public static event NotifierServiceEventHandler OnOk
          {
              add
              {
                  lock (Locker)
                  {
                      _notifierServiceEventHandler += value;
  
                      if (
                          _notifierServiceEventHandler.GetInvocationList()
                                                      .Count(
                                                          _ =&gt;
                                                          _.Method.DeclaringType != null &amp;&amp;
                                                          value.Method.DeclaringType != null &amp;&amp;
                                                          _.Method.DeclaringType == value.Method.DeclaringType) &lt;= 1)
                          return;
  
                      _notifierServiceEventHandler -= value;
                  }
              }
              remove
              {
                  lock (Locker)
                  {
                      _notifierServiceEventHandler -= value;
                  }
              }
          }
  
          // and many more events similar to previous...
  
          #endregion
  
          #region Event firing methods
  
          public static void NotifyOk(string fullMessage = "Ok.", string shortMessage = null)
          {
              NotifierServiceEventHandler handler;
  
              lock (Locker)
              {
                  handler = _notifierServiceEventHandler;
              }
  
              if (handler == null) return;
  
              handler(typeof (NotifierService),
                      new NotifierServiceEventArgs(StatusType.Ok, fullMessage, shortMessage ?? fullMessage));
          }
  
          #endregion
      }
  </code></pre>
  
  <p>So in some places of code these events could be fired like:</p>
  
  <pre><code>NotifierService.NotifyExclamation("Fail!");
  </code></pre>
  
  <p>In the main form there is <code>StatusStrip</code> control used for notification purposes, and due to main form has subscribtion to these events -- their messages will be shown in the status strip.<br />
  BUT!, as I've said earlier, user may open other forms, and these forms could produce others and so on... (they are derived from one class <code>ManagedForm</code> which will be subscribed to <code>NotifierService</code> as soon as it has been created).<br />
  In these forms there is another logic how to notify user -- they need to show <code>MessageBox</code>es with messages. As you can see, I've added some magic in event accessors to allow only one subscriber of any type, because w/o this all opened forms will generate their own <code>MessageBox</code>es. But when one child <code>ManagedForm</code> has produced another and the second has been closed -- no <code>MessageBox</code>es will be shown.<br />
  What magic should I implement to allow subscription from only first <code>ManagedForm</code>? Many thanks for any ideas.</p>
  
  <p><strong>EDIT:</strong> Suggested ideas doesn't solve this issue. I've tried to change event to this:</p>
  
  <pre><code>private static readonly object Locker = new object();
  
  private static EventHandler&lt;NotifierServiceEventArgs&gt; _myEvent;
  
  public static event EventHandler&lt;NotifierServiceEventArgs&gt; OnOk
  {
      add
      {
          if (_myEvent == null || _myEvent.GetInvocationList().All(_ =&gt; _.Method.DeclaringType != value.Method.DeclaringType))
          {
              _myEvent += value;
          }
      }
      remove
      {
          _myEvent -= value;
      }
  }
  </code></pre>
  
  <p>Then I've open one modal child form and create a situation in which event has been fired by <code>NotifierService</code>. One <code>MessageBox</code> has been generated and shown (that's OK). Afterwards I've opened another modal form from first and create another situation in which another event has been fired. One <code>MessageBox</code> has been generated and shown (that's also OK). Now I'm closing second form and making a situation needed to fire event. No <code>MessageBox</code>es has been shown (but in the status strip of the main form message of event has been shown correctly, so nothing has been changed from my first implementation).<br />
  Should I change something in <code>remove</code> clause? I do not need that <strong>only one subscriber</strong> should be, I need that each of the subscribers <strong>should be of distinct types</strong>. Sorry If bad English.</p>