geirsagberg / PropertyChanged

Injects INotifyPropertyChanged code into properties at compile time

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is an add-in for Fody

Icon

Injects INotifyPropertyChanged code into properties at compile time.

Introduction to Fody

Note: NotifyPropertyWeaver

Users of the NotifyPropertyWeaver extension who are migrating to Fody will want to use NuGet to Install the PropertyChanged.Fody package along with Fody itself to get the same functionality as before. This is because Fody is a general purpose weaver with plugins while NotifyPropertyWeaver was specific to one scenario. That scenario now lives in the PropertyChanged addin. See Converting from NotifyPropertyWeaver for more information

Nuget

Nuget package http://nuget.org/packages/PropertyChanged.Fody

To Install from the Nuget Package Manager Console

PM> Install-Package PropertyChanged.Fody

Your Code

[ImplementPropertyChanged]
public class Person 
{        
    public string GivenNames { get; set; }
    public string FamilyName { get; set; }

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }
}

What gets compiled

public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

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

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

    public string FullName
    {
        get
        {
            return string.Format("{0} {1}", GivenNames, FamilyName);
        }
    }

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

Icon

Icon courtesy of The Noun Project

Contributors

More Info

About

Injects INotifyPropertyChanged code into properties at compile time

License:MIT License