Share via

Microsoft.CodeAnalysis.NetAnalyzers not doing anything at all

Endasil 31 Reputation points
2021-02-03T21:07:29.353+00:00

So I switched from using the fxcop nuget to the new Microsoft.CodeAnalysis.NetAnalyzers NuGet and it does nothing at all. I added the following to my csproj file expecting it to apply all rules but nothing happens when i build. I uploaded an example project https://easyupload.io/iktg0k

I added the following to my csproj file
<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
</PropertyGroup>

my application looks like this and should generate https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1819

namespace ConsoleApp1  
{  
    public class Book  
    {  
        private string[] _Pages;  
  
        public Book(string[] pages)  
        {  
            _Pages = pages;  
        }  
  
        public string[] Pages  
        {  
            get { return _Pages; }  
        }  
    }  
  
    class Program  
    {  
        static void Main(string[] args)  
        {  
        }  
    }  
}  
Developer technologies | C#
Developer technologies | 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.

0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. OK_MF 20 Reputation points
    2026-02-10T08:47:18.11+00:00

    When you install an analyzer package, the analyzer is used in the compilation automatically. But it won't have any rules. For .NET core projects the SDK brings in default rules (the rule format is now .editorconfig files.) Observe the /analyzerconfig and /analyzer flags on the csc.exe command line.

    There is a stack overflow answer about this: https://stackoverflow.com/a/79886462/321491

    You need rules like dotnet_diagnostic.CA1000.severity = warning.

    0 comments No comments

  2. Timon Yang-MSFT 9,606 Reputation points
    2021-02-04T06:33:10.377+00:00

    Although the documentation says that the analyzers work for projects that target net5.0 as well as earlier .NET versions, such as netcoreapp3.1 and net472.

    But when I tested it, it took effect immediately after using it in the .Net Core3.1 project and adding the configuration information you provided.
    63836-1.png
    But for .Net Framework 4.7.2, it is always useless. I'm not sure if this is a design flaw of the package or different configuration information is required in the .Net framework.

    I suggest you ask for confirmation in its GitHub repository.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

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