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.
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
- Listens for
IdentifierNameSyntaxnodes. - Checks if the symbol is marked
[Obsolete]. - Extracts the message (e.g.,
"Use NewProp instead"). - Reports a diagnostic (
ROB001).
- 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.
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 ReplaceObsoleteOr manually include the .nupkg as an Analyzer reference:
- Right-click your project β Add β Analyzer...
- Select
ReplaceObsoleteAnalyzer.dllor the.nupkgfile. - Build your project β diagnostics will automatically appear in Visual Studio or
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:
- 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>- .NET 8 SDK
- Visual Studio 2022 (with Roslyn SDK workload)
dotnet build
dotnet testdotnet pack ReplaceObsolete.Package/ReplaceObsolete.Package.csproj -c ReleaseThe package will be located in:
ReplaceObsolete.Package/bin/Release/
- 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.
This project is licensed under the MIT License.
Contributions, issues, and feature requests are welcome!
Please open an issue or submit a pull request.