title: Unsubscribe from original event when try unsubscribe from a variable of that EventHandler
body: <p>I'm trying to unsubscribe attached method to my event. I have this code:</p>
  <pre><code>public event EventHandler MenuItemSelectionChanged;
  
  var eh = MenuItemSelectionChanged;
  eh -= TheMethod;
  </code></pre>
  <p>After run the above code result is:</p>
  <blockquote>
  <p>MenuItemSelectionChanged:  Is Not Null</p>
  <p>el: Is Null</p>
  </blockquote>
  <p>But I want to unsubscribe <code>TheMethod</code> from <code>MenuItemSelectionChanged</code>, by using <code>eh</code> (a pointer to the EventHandler). How can I use <code>eh</code> to remove <code>TheMethod</code> from <code>eh</code> and <code>MenuItemSelectionChanged</code></p>
  <p>I need this result:</p>
  <blockquote>
  <p>MenuItemSelectionChanged:  Is Null</p>
  <p>el: Is Null</p>
  </blockquote>
  <p>My main scenario is:</p>
  <p>Inside <code>TargetClass.cs</code>:</p>
  <pre><code>    public event EventHandler MenuItemSelectionChanged;
  
      public void UnsubscribeFromEvent(ThisClassEventHandlerNames eventHandlerName, Dictionary&lt;FrameworkElement, MethodInfo&gt; eventHandlerInfos)
      {
          var eh = GetEventHandler(eventHandlerName);
          if (eh == null) return;
  
          foreach (var eventHandlerInfo in eventHandlerInfos)
          {
              var info = eventHandlerInfo;
  
              if (eh == null)
                  break;
              
                  var invocationList = eh.GetInvocationList().Where(x =&gt;
                      Equals(x.Target, info.Key) &amp;&amp;
                      x.Method == info.Value);
  
              foreach (var myDeligate in invocationList)
              {
                  if (eh == null)
                      break;
  
                  // I HAVE A PROBLEM HERE&gt;&gt;
                  eh -= (EventHandler) myDeligate;
                  // RESULT: eh is null BUT MenuItemSelectionChanged is not null
              }
          }
      }
  
      public enum ThisClassEventHandlerNames
      {
          MenuItemSelectionChanged
      }
  
      private EventHandler GetEventHandler(ThisClassEventHandlerNames eventHandlerName)
      {
          EventHandler result = null;
          switch (eventHandlerName)
          {
              case ThisClassEventHandlerNames.MenuItemSelectionChanged:
              {
                  result = MenuItemSelectionChanged;
                  break;
              }
              default:
              {
                  result = null;
                  break;
              }
          }
          return result;
      }
  </code></pre>
  <p>I call the above code in my wpf user control with some codes like this:</p>
  <p>Inside <code>MyUserControl.cs</code>:</p>
  <pre><code>TargetClassObject.UnsubscribeFromEvent(
      TargetClass.ThisClassEventHandlerNames.MenuItemSelectionChanged,
      new Dictionary&lt;FrameworkElement, MethodInfo&gt;
      {
          {
              this,
              Infrastructure.Utility.GetMethodInfo&lt;MyUserControl&gt;(x =&gt; x.MenuControl_MenuItemSelectionChanged(null, null))
          }
      });
  </code></pre>
  <p>Really I want to unsubscribe a method from an event by call a method inside that class (<code>targetClass</code>) by this parameters:</p>
  <blockquote>
  <p><strong>A)</strong> An enum value (to get event by it)</p>
  <p><strong>B)</strong> A method info (to unsubscribe its method
  from the event)</p>
  </blockquote>
  <p>Note: My platform is C#, WPF, .NET Framework 4.5</p>