Anklor / PropertyChanged

Injects INotifyPropertyChanged code into properties at compile time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chat on Gitter NuGet Status

Icon

Injects code which raises the PropertyChanged event, into property setters of classes which implement INotifyPropertyChanged.

This is an add-in for Fody; it is available via NuGet:

PM> Install-Package PropertyChanged.Fody

Overview

NOTE: All classes that implement INotifyPropertyChanged will have notification code injected into property setters.

Your code:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    
    public string GivenNames { get; set; }
    public string FamilyName { get; set; }
    public string FullName => $"{GivenNames} {FamilyName}";
}

What gets compiled:

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    string givenNames;
    public string GivenNames
    {
        get => givenNames;
        set
        {
            if (value != givenNames)
            {
                givenNames = value;
                OnPropertyChanged("GivenNames");
                OnPropertyChanged("FullName");
            }
        }
    }

    string familyName;
    public string FamilyName
    {
        get => familyName;
        set 
        {
            if (value != familyName)
            {
                familyName = value;
                OnPropertyChanged("FamilyName");
                OnPropertyChanged("FullName");
            }
        }
    }

    public string FullName => $"{GivenNames} {FamilyName}";

    public virtual void OnPropertyChanged(string propertyName)
    {
        var propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Notes

  • Dependent properties -- In the above sample, the getter for FullName depends on the getters for GivenName and FamilyName. Therefore, when either GivenName or FamilyName is set, PropertyChanged is raised for FullName as well. This behavior can be configured manually using the AlsoNotifyFor attribute on the source property, or the DependsOn attribute on the target property).

  • Intercepting the notification call

    • Global interception
    • Class-level interception --The OnPropertyChanged method will only be injected if there is no such existing method on the class; if there is such a method, then calls to that method will be injected into the setters -- see here.
    • Property-level interception -- For a given property, if there is a method of the form On<PropertyName>Changed, then that method will be called -- see here.
  • To get the before / after values, use the following signature for OnPropertyChanged / On<PropertyName>Changed:

    public void OnPropertyChanged(string propertyName, object before, object after)
    
  • To prevent a specific class from having the notification call injection, use the DoNotNotify attribute.

  • The INotifyPropertyChanged interface can be automatically implemented for a specific class using the AddINotifyPropertyChangedInterfaceAttribute attribute. Raising an issue about "this attribute does not behave as expected" will result in a RTFM and the issue being closed.

  • Behavior is configured via attributes, or via options in the Weavers.xml file.

For more information, see the wiki pages.

Contributors

Icon courtesy of The Noun Project

About

Injects INotifyPropertyChanged code into properties at compile time

License:MIT License


Languages

Language:C# 98.2%Language:PostScript 1.4%Language:F# 0.4%