Skip to content

NavneetHegde/ReplaceObsoleteAnalyzer

Repository files navigation

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


About

A Roslyn analyzer and code fix provider that detects obsolete members and replaces them with their suggested alternatives.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors