Search code examples
wpfpostsharp

PostSharp NotifyPropertyChanged Model - PropertyChangedEventHandler


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?


Solution

  • 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;