Walkthrough: Automatically Implementing INotifyPropertyChanged

This section shows how to make your class automatically implements the INotifyPropertyChanged interface NotifyPropertyChangedAttribute aspect.

Let's start with a simple class that has two simple properties and one composite property:

C#
public class CustomerForEditing 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public string FullName  
    {  
        get { return string.Format("{0} {1}", this.FirstName, this.LastName);} 
    } 
}

This topic contains the following sections:

Adding the NotifyPropertyChanged aspect with PostSharp Tools for Visual Studio

To add INotifyPropertyChanged aspect with PostSharp Tools for Visual Studio:

  1. Put the caret on the class name and expand the Smart Tag. From the list select "Implement INotifyPropertyChanged".

    INotify Property Changed 1
  2. If you haven't previously added the Model Pattern Library to the current project, PostSharp will inform you that it will be doing this as well as adding an attribute to the target class.

    INotify Property Changed 2
  3. PostSharp will download the Model Pattern Library and add the attribute.

    INotify Property Changed 3
  4. Once the download, installation and configuration of the Model Pattern Library and the addition of the attribute has finished you can close the wizard and look at the changes that were made to your codebase.

    INotify Property Changed 4
  5. You'll notice that the code you added NotifyPropertyChangedAttribute to has only been slightly modified. PostSharp has added a NotifyPropertyChangedAttribute attribute to the class. This class level attribute will add the implementation of NotifyPropertyChangedAttribute to the class as well as the plumbing code in each property that makes it work.

    C#
    [NotifyPropertyChanged] 
    public class CustomerForEditing 
    { 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    
        public string FullName  
        {  
            get { return string.Format("{0} {1}", this.FirstName, this.LastName); }  
        } 
    
    }
    Note Note

    This example has added NotifyPropertyChangedAttribute to one class. If you need to implement NotifyPropertyChangedAttribute to many different classes in your codebase you will want to read about using aspect multicasting. See the section Adding Aspects to Multiple Declarations.

By using the Model Pattern Library to add NotifyPropertyChangedAttribute to your Model classes you are able to eliminate all of the repetitive boilerplate coding tasks and code from the codebase.

Adding the NotifyPropertyChanged aspect manually

The wizard does nothing more than installing a NuGet package and adding a custom attribute. You can achieve the same manually.

To add INotifyPropertyChanged aspect manually:

  1. Use NuGet Package Manager to add the PostSharp.Patterns.Model package to your project.

  2. Import the PostSharp.Patterns.Model namespace into your file.

  3. Add the [NotifyPropertyChanged] custom attribute to the class.

Consuming the INotifyPropertyChanged interface

Since the INotifyPropertyChanged interface is implemented by PostSharp at build time after the compiler has completed, the interface will neither be visible to Intellisense or other tools like Resharper, neither to the compiler. The same is true for the PropertyChanged event.

In many cases, this limitation does not matter because the interface is consumed from a framework (like WPF) that is not coupled with your project. However, in some situations, you may need to access the INotifyPropertyChanged interface.

There are two ways to access the INotifyPropertyChanged interface from your code:

  • You can cast your object to INotifyPropertyChanged, for instance:

    C#
    ((INotifyPropertyChanged) obj).PropertyChanged += obj_OnPropertyChanged;

    If your tooling complains that the object does not implement the interface, you can first cast to object:

    C#
    ((INotifyPropertyChanged) (object) obj).PropertyChanged += obj_OnPropertyChanged;
  • You can use the Post.Cast<SourceType, TargetType>(SourceType) method. The benefit of using this method is that the cast operation is validated by PostSharp, to the build will fail if you try to cast an object that does not implement the INotifyPropertyChanged interface. For instance:

    C#
    Post.Cast<Foo,INotifyPropertyChanged>(obj).PropertyChanged += obj_OnPropertyChanged;
See Also