question

JamesJ-9502 avatar image
0 Votes"
JamesJ-9502 asked cooldadtx answered

To enable NETAnalyzer do we need to install separate package?

Recently we have upgraded from VS 2017 to Visual Studio 2019 version 16.8 and also .NET 4.8 framework. Initially we have used fxCop Code analysis, Which needs to be migrated. Do we need to install separate package to enable NETAnalyzer or it is good to have the below settings in the project file. Can you please let me know the process to enable NETAnalyzer in VS 2019.

Enable code analysis on projects that target earlier .NET versions by setting the EnableNETAnalyzers property to true.

dotnet-csharpvs-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

Analyzers are sort of a mess right now IMHO. If you read this article at MS then it tells you which packages to use but those packages are listed as deprecated. The runtime you are targeting is important here.

The main analyzer package seems to be Microsoft.CodeAnalysis.NETAnalyzers. If you are targeting .NET Framework in your project then you need to add a reference to the nuget package. If you are targeting .NET 5 then it is included automatically. If you are targeting .NET Core/Standard then set the EnableNETAnalyzers property to true. In my experience you also need to add the nuget package right now. All this is somewhat documented here. If you get a build warning then remove the project property.

Note that analyzer rules change over time so refer to this doc for what is currently available. Some are disabled and would need to be turned on. Not all the FxCop rules are converted. Some are no longer best practices or applicable while others have not been converted. You could include the deprecated FxCop package if you wanted but it might conflict with the new analyzers.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.