title: INotifyPropertyChanged subscribing to self
body: <p>I have a class which implements INotifyPropertyChanged.  I'm using Simon Cropp's excellent <a href="http://github.com/SimonCropp/NotifyPropertyWeaver" rel="nofollow noreferrer">NotifyPropertyWeaver</a> to inject INotifyPropertyChanged code into the properties.  However I now need to modify the setters for a bunch of properties to all do the same thing after the set completes.  Modifying the setters requires creating backing fields, implementing the get, implementing the set, etc all with identical implementations except the name of the backing field.  I just avoided doing all of this by using <a href="http://github.com/SimonCropp/NotifyPropertyWeaver" rel="nofollow noreferrer">NotifyPropertyWeaver</a>.</p>
  
  <p>Instead, I could have my class subscribe to it's own PropertyChanged event and handle the post change operation in the event handler.  Is this a safe thing to do?  I realize I'll have to watch out for infinite recursion resulting in a stack overflow if my operation modifies one of the properties I'm watching.  Are there any other issues I need to watch out for?</p>
  
  <p>Here's an example</p>
  
  <pre><code>public class Foo : INotifyPropertyChanged{
      public event PropertyChangedEventHandler PropertyChanged;
  
      public Prop1 { get; set; }
      public Prop2 { get; set; }
      public Prop3 { get; set; }
      public Prop4 { get; set; }
  
      public Foo(){
          this.PropertyChanged +=
              new PropertyChangedEventHandler(Foo_PropertyChanged);
      }
  
      private void Foo_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
          switch (e.PropertyName)
          {
              case "Prop1":
              case "Prop2":
              case "Prop3":
                  DoSomething();
                  break;
              case "Prop4":
                  DoSomethingElse();
                  break;
          }
      }
  
      private void DoSomething()
      {
          ....
      }
  
      private void DoSomethingElse()
      {
          ....
      }
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p><a href="https://stackoverflow.com/users/645/joel-lucsy">Joel Lucsy</a> kindly pointed out that I do not need to subscribe to the event.  I had been allowing NotifyPropertyWeaver to inject OnPropertyChanged.  Instead I could just implement OnPropertyChanged myself to accomplish the same thing.</p>
  
  <p>Updated code:</p>
  
  <pre><code>public class Foo : INotifyPropertyChanged{
      public event PropertyChangedEventHandler PropertyChanged;
  
      public Prop1 { get; set; }
      public Prop2 { get; set; }
      public Prop3 { get; set; }
      public Prop4 { get; set; }
  
      private void OnPropertyChanged(string propertyName)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
              handler(this, new PropertyChangedEventArgs(propertyName));
          switch (propertyName)
          {
              case "Prop1":
              case "Prop2":
              case "Prop3":
                  DoSomething();
                  break;
              case "Prop4":
                  DoSomethingElse();
                  break;
          }
      }
  
      private void DoSomething()
      {
          ....
      }
  
      private void DoSomethingElse()
      {
          ....
      }
  }
  </code></pre>