如何从调用方法的InvocationExpressionSyntax节点获取IMethodSymbol或方法声明的语法节点?

时间:2017-06-29 19:16:29

标签: c# roslyn roslyn-code-analysis

在我的分析器的实现中,我使用AnalyzeSymbol方法:

public override void Initialize(AnalysisContext context)
{
  context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method);
}

private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
  // here I look through the syntax of the method body in order to find invocations.
  var methodSymbol = context.Symbol as IMethodSymbol;
  var location = methodSymbol.Locations.FirstOrDefault();
  var methodNode = location.SourceTree.GetCompilationUnitRoot().FindNode(locati‌​on.SourceSpan);
  var blockNode = methodNode.ChildNodes().FirstOrDefault(s => s is BlockSyntax);
  var invocations = blockNode.DescendantNodes()
    .Select(s => s as InvocationExpressionSyntax)
    .Where(s => s != null)

  foreach(InvocationExpressionSyntax i in invocations i)
  {
    // look at the signature of the method which is invoked
    // in order to figure out if the arguments are attributed
  }
}

对于我找到的每个InvocationExpressionSyntax节点,我需要检查在调用调用的声明方法的签名中声明参数的属性(即我的[ReadOnly]属性,请参阅here )。根据我的理解,我需要以某种方式从InvocationExpressionSyntax对象到相应的IMethodSymbol,但我无法弄清楚如何。与FieldDeclarationSyntax具有属性Declarations的{​​{1}}不同,IFieldSymbol就像发布here一样,InvocationExpressionSyntax没有,而我尝试的以下代码会产生null符号:

var model = context.Compilation.GetSemanticModel(ies.SyntaxTree);
var symbol = model.GetDeclaredSymbol(ies.Expression);

我做错了什么?

我的分析器应该分析的示例代码是:

void Foo()
{
  int i = 42;

  // coming from this InvocationExpressionSyntax node I want to find out that i is an parameter for an argument which has an [ReadOnly] attribute
  Bar(i);

  // coming from this InvocationExpressionSyntax node I want to find out that i is an parameter for an argument which has NOT an [ReadOnly] attribute
  Baz(i);
}

void Bar([ReadOnly] int i)
{
}

void Baz(int i)
{
}

1 个答案:

答案 0 :(得分:2)

您可以使用InvocationExpressionSyntax

获取SymbolFinder的声明符号
Workspace workspace;
Workspace.TryGetWorkspace(location.SourceTree.GetText().Container, out workspace);
if (workspace != null)
{
    var invocMethodSymbol = model.GetSymbolInfo(ies.Expression).Symbol;
    // Await this task if you can
    var declarationMethodSymbol = SymbolFinder.FindSourceDefinitionAsync(invocMethodSymbol, workspace.CurrentSolution).Result;
}