gpt4 book ai didi

c# - Roslyn:如何获取与标识符关联的 ITypeSymbol?

转载 作者:行者123 更新时间:2023-11-30 16:45:17 30 4
gpt4 key购买 nike

我正在尝试编写一个 Roslyn 分析器来检测在数组上调用 Enumerable.Count() 的用法。这是我的分析器中的相关代码:

public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeInvocationExpression, SyntaxKind.InvocationExpression);
}

private static void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context)
{
var invocation = (InvocationExpressionSyntax)context.Node;
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (!memberAccess.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
return;
}

Debug.Assert(memberAccess != null);
var ident = memberAccess.Name.Identifier;
// Check whether the method is Count() and there is no parameter list before we try to use the Symbol APIs.
if (ident.ToString() != nameof(Enumerable.Count))
{
return;
}

var arguments = invocation.ArgumentList.Arguments;
if (arguments.Count > 0)
{
return;
}

// Make sure that the subject is an array.
var subject = memberAccess.Expression;
var subjectSymbol = context.SemanticModel.GetSymbolInfo(subject).Symbol;
if (subjectSymbol == null)
{
return;
}

// ???
}

我无法确定调用 Count() 的对象是否是一个数组。我扫描了一下 API,发现有一个 ILocalSymbol 带有 Type 属性,还有一个 IFieldSymbol 带有 Type 属性,这两个属性都可能会为您提供对象的类型。但是,我不知道我正在分析的对象是否是方法调用/等的本地/字段/结果,所以我希望 IFieldSymbolILocalSymbol 到例如共享一些通用的基本接口(interface),例如 IVariableSymbol,它为您提供 Type,而无需知道变量可能来自的所有可能位置。但是,这两个接口(interface)似乎都直接派生自 ISymbol

最好的解决方案就是做这样的事情吗?

internal static class SymbolUtilities
{
public static ITypeSymbol GetType(ISymbol symbol)
{
if (symbol is IFieldSymbol)
{
return ((IFieldSymbol)symbol).Type;
}

if (symbol is ILocalSymbol)
{
return ((ILocalSymbol)symbol).Type;
}

...
}
}

最佳答案

您可以使用 SemanticModel 类的方法 GetTypeInfo 获取有关类型的信息:

ITypeSymbol subjectType = context.SemanticModel.GetTypeInfo(subject).Type;

您将在文章中找到更多详细信息

"Introduction to Roslyn and its use in program development "

关于c# - Roslyn:如何获取与标识符关联的 ITypeSymbol?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42463667/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com