PropertyChanged.Fody Documentation

repository·master·Indexed 24 days ago

https://github.com/fody/propertychanged

A Fody add-in that automatically injects code into property setters to raise the PropertyChanged event for classes implementing INotifyPropertyChanged in C#. It includes a C# code generator for boilerplate reduction, support for dependent properties via [AlsoNotifyFor] and [DependsOn], and customizable notification interception.

Tokens
1.3K
Snippets
6
Records
7
Agent score
34%

What's inside PropertyChanged.Fody

  1. Advanced Notification Customization

    master

    PropertyChanged.Fody provides several ways to intercept or customize how notifications are raised:

    • Dependent Properties: Use the [AlsoNotifyFor] attribute on the source property or [DependsOn] on the target property to manage dependencies.
    • Interception:
      • Class-level: If you define your own OnPropertyChanged method, the weaver will inject calls into your existing method instead of creating a new one.
      • Property-level: If a method named On<PropertyName>Changed exists, it will be called.
    • Before/After Values: To access the previous and new values during notification, use the signature: public void OnPropertyChanged(string propertyName, object before, object after).
    • Opt-in/Opt-out:
      • Use [DoNotNotify] to prevent injection in a specific class.
      • Use [assembly: PropertyChanged.FilterType("Regex")] to switch from global injection to an opt-in model where only classes matching the regex are processed.
  2. How PropertyChanged.Fody works

    master

    PropertyChanged.Fody automatically injects code into the property setters of any class that implements INotifyPropertyChanged.

    When a property value is changed, the injected code checks if the new value is different from the old one. If it is, it updates the backing field and raises the PropertyChanged event. It also intelligently handles dependent properties (e.g., if FullName depends on GivenNames, changing GivenNames will also trigger a notification for FullName).

    // 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}";
    }
  3. Use the C# Code Generator for INotifyPropertyChanged

    master

    Starting with version 4, you can use the C# code generator to automatically create the boilerplate for INotifyPropertyChanged (the event and the invoker methods).

    To use it, mark your class as partial and ensure it implements INotifyPropertyChanged (or use the [AddINotifyPropertyChangedInterface] attribute).

    Requirements:

    • Only classes are supported (no records).
    • For nested classes, all containing classes must also be partial.
    • Works correctly only in SDK-style projects.
    {
        public int Property1 { get; set; }
        public int Property2 { get; set; }
    }
  4. Install PropertyChanged.Fody via NuGet

    master

    To use PropertyChanged.Fody, you must install both the Fody package and the PropertyChanged.Fody package. It is recommended to install Fody explicitly to ensure you are using a recent version.

    Run the following commands in the NuGet Package Manager Console:

    PM> Install-Package Fody
    PM> Install-Package PropertyChanged.Fody
  5. Configure the Code Generator via Project File

    master
    • IsCodeGeneratorDisabled: Set to true to disable the code generator.
    • EventInvokerName: Allows you to rename the generated event invoker method (defaults to OnPropertyChanged).
    <PropertyGroup>
      <PropertyChangedAnalyzerConfiguration>
        <IsCodeGeneratorDisabled>false</IsCodeGeneratorDisabled>
        <EventInvokerName>OnPropertyChanged</EventInvokerName>
      </PropertyChangedAnalyzerConfiguration>
    </PropertyGroup>
  6. Fix duplicate analyzer errors in WPF projects

    master

    WPF projects targeting multiple frameworks may encounter compilation errors (CS0111) due to duplicate OnPropertyChanged members in generated temporary projects. You can resolve this by adding the following target to your project file:

    <Target Name="RemoveDuplicateAnalyzers" BeforeTargets="CoreCompile">
      <!-- see https://github.com/dotnet/wpf/pull/6680 -->
      <RemoveDuplicates Inputs="@ (Analyzer)">
        <Output
          TaskParameter="Filtered"
          ItemName="FilteredAnalyzer"/>
      </RemoveDuplicates>
      <ItemGroup>
        <Analyzer Remove="@ (Analyzer)" />
        <Analyzer Include="@ (FilteredAnalyzer)" />
      </ItemGroup>
    </Target>