c# - Visual Studio 代码分析器 : Finding types with zero references

标签 c# visual-studio-2015 roslyn

我正在尝试编写一个代码分析器来查找未从 Visual Studio 2015 解决方案中的任何其他类型引用的类型。

我的问题是我不知道如何找到未引用类型的列表。

我已经尝试过 DOM,正如您从下面的代码中看到的那样,但我不知道要导航到哪里,而且当前的代码看起来已经很慢了。

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using System.Linq;

namespace AlphaSolutions.CodeAnalysis
{
    [DiagnosticAnalyzer(LanguageNames.CSharp)]
    public class ZeroReferencesDiagnosticAnalyzer : DiagnosticAnalyzer
    {
        public const string DiagnosticId = "ZeroReferences";

        private static DiagnosticDescriptor rule = new DiagnosticDescriptor(
            DiagnosticId,
            title: "Type has zero code references",
            messageFormat: "Type '{0}' is not referenced within the solution",
            category: "Naming",
            defaultSeverity: DiagnosticSeverity.Warning,
            isEnabledByDefault: true,
            description: "Type should have references."
            );

        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
        {
            get
            {
                return ImmutableArray.Create(rule);
            }
        }

        public override void Initialize(AnalysisContext context)
        {
            context.RegisterSyntaxNodeAction(this.AnalyzeSyntaxNode, SyntaxKind.ClassDeclaration);
        }

        private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext obj)
        {
            var classDeclaration = obj.Node as ClassDeclarationSyntax;
            if(classDeclaration == null)
            {
                return;
            }

            var identifierNameSyntaxes = classDeclaration
                .DescendantNodes()
                .OfType<IdentifierNameSyntax>()
                .ToArray()
                ;
            if (identifierNameSyntaxes.Length == 0)
            {
                return;
            }

            //SymbolFinder.FindReferencesAsync(namedTypeSymbol, solution);
        }
    }
}

我也尝试过 SymbolFinder.FindReferencesAsync(namedTypeSymbol, solution) 但我不知道如何获取对 Solution 的引用。

关于 Microsoft Answers 的回复甚至建议使用 Roslyn.Services 程序集中的 FindReferences 方法。但看起来该程序集已被弃用。

我知道 CodeLens 我正在计算引用,访问该计数器可能是一个更好的解决方案,但我猜这是不可能的。

在任何人提出重复帖子之前,这篇帖子不是 this 的副本, thisthis .我的帖子专门针对 Roslyn 编译器的分析器。

最佳答案

Roslyn 诊断分析器目前不允许您进行解决方案范围(即跨项目)分析,这就是我们不为您提供 Solution 对象的原因。这部分是出于性能方面的考虑:如果您尝试在任何地方调用 FindReferencesAsync,您的 CPU 将被严重占用。对于 CodeLens,有大量关于我们使用了多少 CPU 的反馈,我们不希望 10 个诊断都消耗相同数量的 CPU。 (想象一下你糟糕的笔记本电脑电池……)

如果您同意这更受限制,比如查找在它们所在的项目中未使用的内部类型,请查看 this analyzer we wrote awhile back .

关于c# - Visual Studio 代码分析器 : Finding types with zero references,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32566756/

相关文章:

c# - 什么是新的 .Net Native

c# - 关于默认的随机种子,它会在不同的系统中改变吗?

c# - 如何使用 WebSockets 跟踪已建立的连接

javascript - Visual Studio 的 Javascript Intellisense 能否给出有关 'this' 关键字的提示?

c++ - CMake - 为生成的项目定义 dll 目录或文件

c# - 如何使用 roslyn 获取未使用的引用?

c# - 在 C# 中,我可以创建一个变量,根据构成它的变量的变化来更新它的值吗?

c# - 如何修复 'The current thread is not associated with the renderer' 的同步上下文'?

c# - Visual Studio 2015 添加发布版本号

c# - 如何使用新语法树部分更新编译?