如何获取解决方案中的所有分析器规则

How to get all analyzers rules in a solution

我在我的解决方案中通过 NuGet 添加了一些分析器。 如何从 NuGet 引用中获取所有添加的分析器规则? 我需要所有启用的分析器的 ID(例如 CA1001)和描述。

编辑:我需要一些 C# 代码来执行此操作。

您可以在“依赖项”树中查看项目分析器生成的诊断信息:

您可以通过这些叶节点上的上下文菜单调整级别(none、建议、警告、错误等)。

或者,如果您使用的是 VS16.10(最近发布),则有一个新的 .editorconfig 编辑器,其中包含一个显示所有分析器的选项卡:

来自 GitHub 的回答。 jmarolf 的致谢:

参考文献:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.9.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="3.9.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="3.9.0" />
  </ItemGroup>

</Project>

C#:

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;

namespace AnalyzerReader
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Attempt to set the version of MSBuild.
            var instance = MSBuildLocator.RegisterDefaults();

            Console.WriteLine($"Using MSBuild at '{instance.MSBuildPath}' to load projects.");

            using var workspace = MSBuildWorkspace.Create();

            // Print message for WorkspaceFailed event to help diagnosing project load failures.
            workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message);

            var solutionPath = args[0];
            Console.WriteLine($"Loading solution '{solutionPath}'");

            // Attach progress reporter so we print projects as they are loaded.
            var solution = await workspace.OpenSolutionAsync(solutionPath, new ConsoleProgressReporter());
            Console.WriteLine($"Finished loading solution '{solutionPath}'");

            // Get all analyzers in the project
            var diagnosticDescriptors = solution.Projects
                .SelectMany(project => project.AnalyzerReferences)
                .SelectMany(analyzerReference => analyzerReference.GetAnalyzersForAllLanguages())
                .SelectMany(analyzer => analyzer.SupportedDiagnostics)
                .Distinct().OrderBy(x => x.Id);

            Console.WriteLine($"{nameof(DiagnosticDescriptor.Id),-15} {nameof(DiagnosticDescriptor.Title)}");
            foreach (var diagnosticDescriptor in diagnosticDescriptors)
            {
                Console.WriteLine($"{diagnosticDescriptor.Id,-15} {diagnosticDescriptor.Title}");
            }
        }

        private class ConsoleProgressReporter : IProgress<ProjectLoadProgress>
        {
            public void Report(ProjectLoadProgress loadProgress)
            {
                var projectDisplay = Path.GetFileName(loadProgress.FilePath);
                if (loadProgress.TargetFramework != null)
                {
                    projectDisplay += $" ({loadProgress.TargetFramework})";
                }

                Console.WriteLine($"{loadProgress.Operation,-15} {loadProgress.ElapsedTime,-15:m\:ss\.fffffff} {projectDisplay}");
            }
        }
    }
}