检查项目引用的分析器 [英] Analyzer that checks project references

查看:50
本文介绍了检查项目引用的分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个解决方案来控制 C# 项目中的项目引用.理想情况下,此解决方案与 IDE 无关,因此可以与 Visual Studio、Jetbrains Rider 甚至 VS Code 一起使用.这样做的原因是我见过由于人们创建几乎任意的项目引用而完全搞砸的解决方案.在项目发展到一定规模后,很难让他们直接上手.

I'd like to create a solution that controls the project references in C# projects. Ideally, this solution is IDE-agnostic so that it can be used with Visual Studio, Jetbrains Rider, or even VS Code. The reason for this is that I've seen solutions that are completely messed up due to people creating almost arbitrary project references. It's super hard to get them straight after a project has grown to a certain size.

我知道 Visual Studio Enterprise 提供这种开箱即用的功能.不幸的是,在我目前的公司中,我们没有 VS Enterprise.因此,我想自己创建.

I know that Visual Studio Enterprise offers this out-of-the-box. Unfortunately, in my current company we do not have VS Enterprise. Thus, I want to create that on my own.

那么最好的方法是什么?在做了一些研究之后,我认为利用 .NET 编译器平台(Roslyn") 及其 Workspace API 可能是个好主意?似乎我可以将它部署为 NuGet 包,然后可以在任何 IDE 或构建自动化中使用.但也许有更简单或更好的方法,在我开始深入研究之前,我想听听您对此的看法.

So what would be the best way to do it? After doing some research I think leveraging the .NET Compiler Platform ("Roslyn") with its Workspace API might be a good idea? Seems like I could deploy it as a NuGet package which can then be used in any IDE or build-automation. But maybe there's an easier or better way, I'd like to hear your opinion on that before I start digging into it.

另外:如果Roslyn"方式是正确的,是否有一些关于如何创建与 Workspace API 配合使用的分析器的好资源?

Also: if the "Roslyn"-way is the right one is there some good resources on how to create an analyzer that works with the Workspace APIs?

提前致谢.

推荐答案

在你的分析器中,注册一个编译启动动作:

In your analyser, register a compilation start action:

public override void Initialize(AnalysisContext context)
{
    context.RegisterCompilationStartAction(Initialize);
}

private void Initialize(CompilationStartAnalysisContext context)
{
    var compilation = context.Compilation;
}

从该编译对象中,您有多种选择:

From that compilation object, you have various options:

var referencedAssemblyNames = compilation.ReferencedAssemblyNames;

var references = compilation.References;

然后进行分析.要报告诊断,请使用 context.RegisterCompilationEndActionCompilationStartAnalysisContext 上注册一个操作.

Then do your analysis. To report diagnostics, register an action on the CompilationStartAnalysisContext using context.RegisterCompilationEndAction.

如果您不需要查看实际项目内容进行分析,您可以简单地使用 RegisterCompilationAction 如下:

If you don't need to look at actual project content for your analysis, you can simply use RegisterCompilationAction as follows:

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class TooManyReferencesAnalyzer : DiagnosticAnalyzer
{
    private static DiagnosticDescriptor TooManyReferences { get; } =
        new DiagnosticDescriptor(
            "DEMO",
            "Don't use too many references",
            "The project '{0}' has {1} references",
            category: "Maintainability",
            defaultSeverity: DiagnosticSeverity.Warning,
            isEnabledByDefault: true);

    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
        ImmutableArray.Create(TooManyReferences);

    public override void Initialize(AnalysisContext context)
    {
        context.RegisterCompilationAction(AnalyzeCompilation);
    }

    private void AnalyzeCompilation(CompilationAnalysisContext context)
    {
        var compilation = context.Compilation;
        int referenceCount = compilation.References.Count();

        if (referenceCount > 5)
        {
            context.ReportDiagnostic(
                Diagnostic.Create(
                    TooManyReferences,
                    null,
                    compilation.AssemblyName,
                    referenceCount));
        }
    }
}

这篇关于检查项目引用的分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆