To enable NETAnalyzer do we need to install separate package?

James J 586 Reputation points
2021-03-24T10:38:35.387+00:00

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.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,358 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,208 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 56,271 Reputation points
    2021-03-24T14:01:00.187+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.