Migrating to NetAnalyzers

Microsoft makes it very easy to enforce the quality and style rules for your code. This is especially helpful for large organizations.

For a long time Microsoft used Microsoft.CodeAnalysis.FxCopAnalyzers (known as FxCop) to enable and check the rules for .NET projects. As of recently, Microsoft started shipping these analyzers as part of .NET SDK or as a new Nuget package Microsoft.CodeAnalysis.NetAnalyzers

In fact, if you upgrade the FxCop analyzers to the latest version 3.3.2, you will get the following warning message during the compilation:

CSC: warning CA9998: FxCopAnalyzers package has been deprecated in favor of 'Microsoft.CodeAnalysis.NetAnalyzers', that ships with the .NET SDK.

It also gives a link to help with the migration process:

https://docs.microsoft.com/visualstudio/code-quality/migrate-from-fxcop-analyzers-to-net-analyzers

If you follow the link and just replace Microsoft.CodeAnalysis.FxCopAnalyzers with the new Microsoft.CodeAnalysis.NetAnalyzers Nuget package you will stop getting any FxCop/NET Analyzer warnings/errors.

After some error and trial, it turns out you need to set the following properties in your project file:

<PropertyGroup>
  <AnalysisLevel>5</AnalysisLevel>
  <EnableNETAnalyzers>true</EnableNETAnalyzers>
  <AnalysisMode>AllEnabledByDefault</AnalysisMode>
</PropertyGroup>

This has fixed the problem for both Visual Studio 2019 (16.8.4) and Visual Studio Mac (8.8.5). The fix also works for net5.0 projects as well as for netcoreapp3.1

I hope this little tip will save you some time with the migration process to NetAnalyzers

Useful links:

Migrate from legacy analysis (FxCop) to source analysis (.NET analyzers)

Overview of source code analysis

Overview of .NET source code analysis

Developer Community: Source code analyzers from .NET 5.0 SDK have no effect in code editor, only in build



要查看或添加评论,请登录

社区洞察

其他会员也浏览了