ReplaceObsoleteAnalyzer 1.0.2

dotnet add package ReplaceObsoleteAnalyzer --version 1.0.2
                    
NuGet\Install-Package ReplaceObsoleteAnalyzer -Version 1.0.2
                    
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="ReplaceObsoleteAnalyzer" Version="1.0.2">
  <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.
<PackageVersion Include="ReplaceObsoleteAnalyzer" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="ReplaceObsoleteAnalyzer">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ReplaceObsoleteAnalyzer --version 1.0.2
                    
#r "nuget: ReplaceObsoleteAnalyzer, 1.0.2"
                    
#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.
#:package ReplaceObsoleteAnalyzer@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ReplaceObsoleteAnalyzer&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=ReplaceObsoleteAnalyzer&version=1.0.2
                    
Install as a Cake Tool

🧩 ReplaceObsoleteAnalyzer

ReplaceObsoleteAnalyzer is a custom .NET Roslyn Analyzer and Code Fix Provider that detects usages of obsolete members (marked with [Obsolete]) and offers automatic replacements when a suggested alternative is provided.


πŸš€ Overview

This analyzer helps maintain cleaner and more maintainable code by identifying usages of deprecated members and replacing them with the recommended new members as suggested in the [Obsolete] attribute message.

For example:

[Obsolete("Use NewProperty instead")]
public string OldProperty { get; set; }

If your code uses:

var x = obj.OldProperty;

The analyzer reports a diagnostic and provides a one-click fix to replace it with:

var x = obj.NewProperty;

πŸ“¦ Features

βœ… Detects usage of [Obsolete("Use X instead")] members
βœ… Reports a clear diagnostic message (ROB001)
βœ… Provides an automatic code fix (β€œReplace with β€˜X’”)
βœ… Fully compatible with Visual Studio and dotnet build
βœ… Lightweight and supports concurrent execution


🧠 How It Works

Analyzer (ReplaceObsoleteAnalyzer)

  • Listens for IdentifierNameSyntax nodes.
  • Checks if the symbol is marked [Obsolete].
  • Extracts the message (e.g., "Use NewProp instead").
  • Reports a diagnostic (ROB001).

Code Fix Provider (ReplaceObsoleteCodeFixProvider)

  • Parses the obsolete message to find the replacement (e.g., "Use NewProp instead" β†’ NewProp).
  • Registers a code action β€œReplace with β€˜NewProp’”.
  • Automatically replaces the identifier in your source code.

🧰 Example

❌ Before

public class Sample
{
    [Obsolete("Use NewValue instead")]
    public int OldValue { get; set; }

    public int NewValue { get; set; }

    public void Test()
    {
        var x = OldValue;
    }
}

βœ… After Code Fix

public void Test()
{
    var x = NewValue;
}

βš™οΈ Installation

You can install ReplaceObsoleteAnalyzer via NuGet (once published):

dotnet add package ReplaceObsolete

Or manually include the .nupkg as an Analyzer reference:

  1. Right-click your project β†’ Add β†’ Analyzer...
  2. Select ReplaceObsoleteAnalyzer.dll or the .nupkg file.
  3. Build your project β€” diagnostics will automatically appear in Visual Studio or dotnet build.

🧾 Diagnostic Info

ID Category Severity Message Format
ROB001 Maintainability Info Member '{0}' is obsolete. Use '{1}' instead.

🧩 Project Structure

ReplaceObsoleteAnalyzer/
β”‚
β”œβ”€β”€ ReplaceObsolete/                   # Analyzer
β”‚   β”œβ”€β”€ ReplaceObsoleteAnalyzer.cs
β”‚   └── Resources.resx
β”‚
β”œβ”€β”€ ReplaceObsolete.CodeFixes/         # CodeFix provider
β”‚   └── ReplaceObsoleteCodeFixProvider.cs
β”‚
β”œβ”€β”€ ReplaceObsolete.Package/           # NuGet packaging
β”‚   └── ReplaceObsolete.Package.csproj
β”‚
β”œβ”€β”€ ReplaceObsolete.Test/              # Analyzer and CodeFix tests
β”‚   └── ReplaceObsoleteAnalyzerTests.cs
β”‚
β”œβ”€β”€ LICENSE
└── README.md

🧩 NuGet Packaging Details

The packaging project (ReplaceObsolete.Package.csproj) is configured to:

  • Include both analyzer and code fix assemblies under analyzers/dotnet/cs
  • Prevent redundant build outputs
  • Mark as a development dependency
  • Support .NET Standard 2.0 for maximum compatibility

Example configuration:

<Target Name="_AddAnalyzersToOutput">
  <ItemGroup>
    <TfmSpecificPackageFile Include="$(OutputPath)\ReplaceObsolete.dll" PackagePath="analyzers/dotnet/cs" />
    <TfmSpecificPackageFile Include="$(OutputPath)\ReplaceObsolete.CodeFixes.dll" PackagePath="analyzers/dotnet/cs" />
  </ItemGroup>
</Target>

πŸ§‘β€πŸ’» Development

Prerequisites

  • .NET 8 SDK
  • Visual Studio 2022 (with Roslyn SDK workload)

Build and Test

dotnet build
dotnet test

Create NuGet Package

dotnet pack ReplaceObsolete.Package/ReplaceObsolete.Package.csproj -c Release

The package will be located in:

ReplaceObsolete.Package/bin/Release/

🧠 Notes

  • The analyzer only applies to messages starting with "Use ".
    (e.g., [Obsolete("Use NewValue instead")])
  • If the obsolete message does not follow this pattern, the diagnostic will still appear but no code fix will be offered.
  • It supports concurrent execution and skips generated code.

πŸ“„ License

This project is licensed under the MIT License.


🀝 Contributing

Contributions, issues, and feature requests are welcome!
Please open an issue or submit a pull request.


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.0.2 256 10/26/2025
1.0.1 180 10/26/2025
1.0.0 210 10/17/2025

Initial release of ReplaceObsoleteAnalyzer.