ReplaceObsoleteAnalyzer 1.0.2
dotnet add package ReplaceObsoleteAnalyzer --version 1.0.2
NuGet\Install-Package ReplaceObsoleteAnalyzer -Version 1.0.2
<PackageReference Include="ReplaceObsoleteAnalyzer" Version="1.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="ReplaceObsoleteAnalyzer" Version="1.0.2" />
<PackageReference Include="ReplaceObsoleteAnalyzer"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add ReplaceObsoleteAnalyzer --version 1.0.2
#r "nuget: ReplaceObsoleteAnalyzer, 1.0.2"
#:package ReplaceObsoleteAnalyzer@1.0.2
#addin nuget:?package=ReplaceObsoleteAnalyzer&version=1.0.2
#tool nuget:?package=ReplaceObsoleteAnalyzer&version=1.0.2
π§© 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
IdentifierNameSyntaxnodes. - 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:
- Right-click your project β Add β Analyzer...
- Select
ReplaceObsoleteAnalyzer.dllor the.nupkgfile. - 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.
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.
Initial release of ReplaceObsoleteAnalyzer.