Visual Studio 代码分析器:查找具有零引用的类型
Visual Studio Code Analyzer: Finding types with zero references
我正在尝试编写一个代码分析器来查找 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 我正在计算引用,访问该计数器可能是一个更好的解决方案,但我猜这是不可能的。
在有人提出重复 post 之前,这个 post 不是 , this or this 的重复。我的 post 特定于 Roslyn 编译器的分析器。
Roslyn 诊断分析器目前不允许您进行解决方案范围(即跨项目)分析,这就是我们不给您 Solution
对象的原因。这部分是出于性能方面的考虑:如果您尝试在任何地方调用 FindReferencesAsync,您的 CPU 将被严重锁定。对于 CodeLens,有大量关于我们使用了多少 CPU 的反馈,我们不希望 10 个诊断都消耗相同数量的 CPU。 (想象一下你糟糕的笔记本电池...)
如果您同意这更受限制,例如查找在它们所在的项目中未使用的内部类型,请查看 this analyzer we wrote awhile back。
我正在尝试编写一个代码分析器来查找 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 我正在计算引用,访问该计数器可能是一个更好的解决方案,但我猜这是不可能的。
在有人提出重复 post 之前,这个 post 不是
Roslyn 诊断分析器目前不允许您进行解决方案范围(即跨项目)分析,这就是我们不给您 Solution
对象的原因。这部分是出于性能方面的考虑:如果您尝试在任何地方调用 FindReferencesAsync,您的 CPU 将被严重锁定。对于 CodeLens,有大量关于我们使用了多少 CPU 的反馈,我们不希望 10 个诊断都消耗相同数量的 CPU。 (想象一下你糟糕的笔记本电池...)
如果您同意这更受限制,例如查找在它们所在的项目中未使用的内部类型,请查看 this analyzer we wrote awhile back。