3

I have litle problem with PostSharp Implementation of INotifyPropertyChanged. PostSharp added PropertyChangedEventHandler PropertyChanged after compile time, but I need react from C# too.

Model a = new Model();
a.PropertyChanged += a_PropertyChanged;

Model implementation;

[NotifyPropertyChanged]
internal class Model
{
    public string A { get; set; }

    public string B { get; set; }

    public string C { get { return string.Format("{0} - {1}", A, B); } }
}

I tried different ways to add handler,but unsuccessfully. Is there some way to do this?

Peter
  • 33
  • 3

1 Answers1

1

Instance of class decorated by NotifyPropertyChanged can be casted to INotifyPropertyChanged at runtime:

((INotifyPropertyChanged)a).PropertyChanged

There is a helper method Post.Cast to avoid "Suspicious cast" warning:

Post.Cast<Model, INotifyPropertyChanged>(a).PropertyChanged += OnPropertyChanged;
wake-0
  • 3,918
  • 5
  • 28
  • 45
Jakub Linhart
  • 4,062
  • 1
  • 26
  • 42