A Roslyn analyzer and code fix provider that detects obsolete members and replaces them with their suggested alternatives.
$ dotnet add package ReplaceObsoleteAnalyzerReplaceObsoleteAnalyzer 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.
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;
✅ 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
ReplaceObsoleteAnalyzer)IdentifierNameSyntax nodes.[Obsolete]."Use NewProp instead").ROB001).ReplaceObsoleteCodeFixProvider)"Use NewProp instead" → NewProp).public class Sample
{
[Obsolete("Use NewValue instead")]
public int OldValue { get; set; }
public int NewValue { get; set; }
public void Test()
{
var x = OldValue;
}
}
public void Test()
{
var x = NewValue;
}
You can install ReplaceObsoleteAnalyzer via NuGet (once published):
dotnet add package ReplaceObsolete
Or manually include the .nupkg as an Analyzer reference:
ReplaceObsoleteAnalyzer.dll or the .nupkg file.dotnet build.| ID | Category | Severity | Message Format |
|---|---|---|---|
| ROB001 | Maintainability | Info | Member '{0}' is obsolete. Use '{1}' instead. |
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
The packaging project (ReplaceObsolete.Package.csproj) is configured to:
analyzers/dotnet/csExample 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>
dotnet build
dotnet test
dotnet pack ReplaceObsolete.Package/ReplaceObsolete.Package.csproj -c Release
The package will be located in:
ReplaceObsolete.Package/bin/Release/
"Use ".[Obsolete("Use NewValue instead")])This project is licensed under the MIT License.
Contributions, issues, and feature requests are welcome!
Please open an issue or submit a pull request.