GithubHelp home page GithubHelp logo

anthrax3 / propertychanged Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fody/propertychanged

0.0 0.0 0.0 3.67 MB

Injects INotifyPropertyChanged code into properties at compile time

License: MIT License

C# 99.70% F# 0.30%

propertychanged's Introduction

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.

Usage

See also Fody usage.

NuGet installation

Install the PropertyChanged.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package PropertyChanged.Fody
PM> Update-Package Fody

The Update-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <PropertyChanged/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <PropertyChanged/>
</Weavers>

Overview

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

Before 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(InternalEventArgsCache.GivenNames);
                OnPropertyChanged(InternalEventArgsCache.FullName);
            }
        }
    }

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

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

    protected void OnPropertyChanged(PropertyChangedEventArgs eventArgs)
    {
        PropertyChanged?.Invoke(this, eventArgs);
    }
}

internal static class InternalEventArgsCache
{
    internal static readonly FamilyName = new PropertyChangedEventArgs("FamilyName");
    internal static readonly FullName = new PropertyChangedEventArgs("FullName");
    internal static readonly GivenNames = new PropertyChangedEventArgs("GivenNames");
}

(the actual injected type and method names are different)


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.

  • To scope the rewriting only to specific classes, and not the whole Assembly, you can use the FilterType attribute. This changes the general behavior from from opt-out to opt-in. Example: [assembly: PropertyChanged.FilterType("My.Specific.OptIn.Namespace.")]. The string is interpreted as a Regex, and you can use multiple filters. A class will be weaved, if any filter matches.

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

Icon

Icon courtesy of The Noun Project

propertychanged's People

Contributors

0x53a avatar brunojuchli avatar cbayles avatar damianreeves avatar distantcam avatar domasm avatar dunge avatar igorushi avatar jaben avatar jhartmann123 avatar ltrzesniewski avatar mihamarkic avatar ngbrown avatar philvx avatar qmfrederik avatar ravensorb avatar simoncropp avatar tom-englert avatar weitzhandler avatar zspitz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.