Skip to content

DotNetAnalyzers/PropertyChangedAnalyzers

Repository files navigation

PropertyChangedAnalyzers

Join the chat at https://gitter.im/DotNetAnalyzers/PropertyChangedAnalyzers License Build status Build Status NuGet

Roslyn analyzers for INotifyPropertyChanged.

  • 1.x versions are for Visual Studio 2015.
  • 2.x versions are for Visual Studio 2017.
  • 3.x versions are for Visual Studio 2019.

Animation

Id Title
INPC001 The class has mutable properties and should implement INotifyPropertyChanged
INPC002 Mutable public property should notify
INPC003 Notify when property changes
INPC004 Use [CallerMemberName]
INPC005 Check if value is different before notifying
INPC006_a Check if value is different using ReferenceEquals before notifying
INPC006_b Check if value is different using object.Equals before notifying
INPC007 The class has PropertyChangedEvent but no invoker
INPC008 Struct must not implement INotifyPropertyChanged
INPC009 Don't raise PropertyChanged for missing property
INPC010 The property gets and sets a different backing member
INPC011 Don't shadow PropertyChanged event
INPC012 Don't use expression for raising PropertyChanged
INPC013 Use nameof
INPC014 Prefer setting backing field in constructor
INPC015 Property is recursive
INPC016 Notify after update
INPC017 Backing field name must match
INPC018 PropertyChanged invoker should be protected when the class is not sealed
INPC019 Getter should return backing field
INPC020 Prefer expression body accessor
INPC021 Setter should set backing field
INPC022 Comparison should be with backing field
INPC023 Don't use instance equals in setter
INPC024 ReferenceEquals is always false for value types

Using PropertyChangedAnalyzers

The preferable way to use the analyzers is to add the nuget package PropertyChangedAnalyzers to the project(s).

The severity of individual rules may be configured using rule set files in Visual Studio 2015.

Installation

PropertyChangedAnalyzers can be installed using Paket or the NuGet command line or the NuGet Package Manager in Visual Studio 2015.

Install using the command line:

Install-Package PropertyChangedAnalyzers

Updating

The ruleset editor does not handle changes IDs well, if things get out of sync you can try:

  1. Close visual studio.
  2. Edit the ProjectName.rulset file and remove the PropertyChangedAnalyzers element.
  3. Start visual studio and add back the desired configuration.

Above is not ideal, sorry about this. Not sure this is our bug.

When using old style packages.config

Many times running the nuget update command breaks things. The script below can be used to manually update.

var package = "PropertyChangedAnalyzers";
var oldVersion = "2.7.2.0";
var newVersion = "2.7.3";
foreach (var csproj in Directory.EnumerateFiles("C:\\Git\\Path\\To\Sln", "*.csproj", SearchOption.AllDirectories))
{
    string text = File.ReadAllText(csproj);
    if (text.Contains($"{package}.{oldVersion}"))
    {
        // <Analyzer Include="..\packages\PropertyChangedAnalyzers.2.7.2.0\analyzers\dotnet\cs\PropertyChangedAnalyzers.dll" />
        // <Analyzer Include="..\packages\PropertyChangedAnalyzers.2.7.2.0\analyzers\dotnet\cs\Gu.Roslyn.Extensions.dll" />
        File.WriteAllText(csproj, text.Replace($"{package}.{oldVersion}", $"{package}.{newVersion}"));
    }

    // <package id="PropertyChangedAnalyzers" version="2.7.2.0" targetFramework="net46" developmentDependency="true" />
}

foreach (var csproj in Directory.EnumerateFiles("C:\\Tfs\\Coromatic\\BoxEr", "packages.config", SearchOption.AllDirectories))
{
    string text = File.ReadAllText(csproj);
    if (text.Contains($"id=\"{package}\" version=\"{oldVersion}\""))
    {
        // <package id="PropertyChangedAnalyzers" version="2.7.2.0" targetFramework="net46" developmentDependency="true" />
        File.WriteAllText(csproj, text.Replace($"id=\"{package}\" version=\"{oldVersion}\"", $"id=\"{package}\" version=\"{newVersion}\""));
    }
}

Current status

Early alpha, names and IDs may change.

About

Roslyn analyzers for INotifyPropertyChanged

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •