Enable all rules with .NET analyzers in .NET standard projects

Viewed 1785

I am moving my project to the new .NET Analyzers using Rosyln, previously I was using the nuget package. I'm trying to do the process described here: Migrate from FxCop analyzers to .NET analyzers

Now my csproj looks like:

<Project>
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <AnalysisMode>AllEnabledByDefault</AnalysisMode>
  </PropertyGroup>
</Project>

If TargetFramework is net5.0, all rules are return an error, but I cannot make it work when it's set to netstandard2.0. I cannot move to net5.0 because this dll is referenced by a .NET 4.8 project.

2 Answers

If you add <AnalysisLevel>5</AnalysisLevel> to your project file, you can instruct projects which do not target .NET 5.0 to use the same default rules as a project targeting net5.0 would do by default.

You can find more information about this here.

Since moving to .Net 5 is not an option (where the analyzer is enabled automatically), I would suggest using the nuget packageMicrosoft.CodeAnalysis.CSharp that perform the analysis.

Related