SatorImaging.StaticMemberAnalyzer 1.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package SatorImaging.StaticMemberAnalyzer --version 1.0.0                
NuGet\Install-Package SatorImaging.StaticMemberAnalyzer -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SatorImaging.StaticMemberAnalyzer" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SatorImaging.StaticMemberAnalyzer --version 1.0.0                
#r "nuget: SatorImaging.StaticMemberAnalyzer, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SatorImaging.StaticMemberAnalyzer as a Cake Addin
#addin nuget:?package=SatorImaging.StaticMemberAnalyzer&version=1.0.0

// Install SatorImaging.StaticMemberAnalyzer as a Cake Tool
#tool nuget:?package=SatorImaging.StaticMemberAnalyzer&version=1.0.0                

Static Field Analyzer for C# / .NET

Roslyn-based analyzer for C# to provide diagnostics of static fields and properties (not yet) initialization.

  • Wrong order of static field declaration
  • Cross-Referencing Problem of static field across type
  • TODO: static property initialization diagnostics

Analyzer in Action

Cross-Referencing Problem

It is a design bug which makes all things complex. Not only that but also it causes initialization error only when meet a specific condition.

So that it must be fixed even if app works correctly at a moment, to prevent simple but complicated bug which is hard to find in large code base by hand. As you know static fields will never report error when initialization failed!!

It could be happened when reordering field declarations on refactoring large code base.

class A {
    public static int Value = B.Other;
    public static int Other = 310;
}

class B {
    public static int Other = 620;
    public static int Value = A.Other;  // will be '0' not '310'
}

public static class Test
{
	public static void Main()
	{
		System.Console.WriteLine(A.Value);  // 620
		System.Console.WriteLine(A.Other);  // 310
		System.Console.WriteLine(B.Value);  // 0   👈👈👈
		System.Console.WriteLine(B.Other);  // 620

        // when changing class member access order, it works correctly 🤣
        // see the following for details
		//System.Console.WriteLine(B.Value);  // 310  👈 correct!!
		//System.Console.WriteLine(B.Other);  // 620
		//System.Console.WriteLine(A.Value);  // 620
		//System.Console.WriteLine(A.Other);  // 310
	}
}

C# Compiler Initialization Logic

  • A.Value = B.Other;
    • // 'B' initialization is started by member access
    • B.Value = A.Other; // B.Value will be 0 because reading uninitialized A.Other
    • B.Other = 620;
    • // then, assign B.Other value (620) to A.Value
  • A.Other = 310; // initialized here!! this value is not assigned to B.Value

Visual Studio 2019 or Earlier

This analyzer is tested on Visual Studio 2022.

You could use this analyzer on older versions of Visual Studio. To do so, update Vsix project file by following instructions written in memo file and build project.

 
 

Devnote

TODO

  • Implement static property analyzer!!
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 1,944 7/29/2024
1.3.0 206 7/23/2024
1.2.1 1,283 6/2/2024
1.2.0 110 6/2/2024
1.1.0 119 6/2/2024
1.0.0 122 6/1/2024