title: Unsubscribing from anonymous event handler inside an static method (extension method)
body: <p>I have an extension method to subscribe a <code>PropertyChanged</code> event of an object that implements <code>INotifyPropertyChanged</code>.</p>
  
  <p>I would like that the event fires just once. Not more.</p>
  
  <p>This is my method.</p>
  
  <pre><code>public static void OnPropertyChanged&lt;T&gt;(this  INotifyPropertyChanged target, string    propertyName, Action action)
  {
      if (target == null)
      {
          return;
      }
  
      PropertyChangedEventHandler handler = (obj, e) =&gt;
      {
  
          if (propertyName == e.PropertyName)
          {
              action();
          }
  
      };
  
  
      target.PropertyChanged -= handler;
      target.PropertyChanged += handler;
  
  }
  </code></pre>
  
  <p>But it does not work. I cannnot remove the event handler so the event fires every time I call this method.</p>
  
  <p>I have try a different approach. Instead of using annonymous methods, something more traditional, like this:</p>
  
  <pre><code>public static void OnPropertyChanged&lt;T&gt;(this  INotifyPropertyChanged target, string    propertyName, Action action)
  {
      if (target == null)
      {
          return;
      }
  
      target.PropertyChanged -= target_PropertyChanged;
      target.PropertyChanged += target_PropertyChanged;
  
  }
  
  static void target_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
          //do stuff here
      }
  </code></pre>
  
  <p>And it just works fine. The event fires just once, but I also need the Action parameter. I cannot use it with this approach.</p>
  
  <p>Any workaround or different aproach to solve this issue?Is there something strange with anonymous methods inside static methods?</p>
  
  <p>Thanks in advance.</p>