简体   繁体   中英

PostSharp [NotifyPropertyChanged] does not work for properties from base class

I am tryig to do a PoC with PostSharp [NotifyPropertyChanged] feature. Suppose that I have some model assembly containing my model classes (simple POCOs) which does not implement INotifyPropertyChanged and cannot be changed.

namespace Ryu.Model
{
    public class PersonModel
    {
        public int Id { get; set; }

        public string DisplayName { get; set; }

        public DateTime? ModifiedDate { get; set; }
    }
}

And I want to introduce a Wrapper layer, which simply inherits from Model's classes and apply PostSharp's [NotifyPropertyChanged] feature.

namespace Ryu.Wrapper
{
    [NotifyPropertyChanged]
    public class PersonWrapper : PersonModel
    {
    }
}

I was expecting this to work and apply PropertyChanged to all properties of base class (PersonModel). But it does not work. Is there any trick to make this work?

PostSharp weaves additional code into the methods and properties of a class when you apply [NotifyPropertyChanged] aspect to that class.

In your case the properties of PersonModel are not modified, because it does not have the aspect applied to it. And I understand that you would not want to apply any changes to PersonModel class. However, the properties are derived as-is by PersonWrapper and so they cannot behave differently in these two classes.

However, if the case is that you would like to add INotifyPropertyChanged implementation to PersonModel but you don't have the source code of the assembly, then you can process the existing assembly by running PostSharp on the command-line. This will produce a new assembly with the aspects weaved into the selected classes. Please refer to the documentation for more information: http://doc.postsharp.net/command-line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM