如何在Roslyn (Microsoft CodeAnalysis)的作用域中获得所有可见的局部变量名

本文关键字:变量名 局部 作用域 Roslyn Microsoft CodeAnalysis | 更新日期: 2023-09-27 18:02:15

(请注意:这不是关于运行时反射/元信息)

我正在写一个Roslyn CSharpSyntaxVisitor的具体实现

当实现VisitIdentifierName

public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax name)
{
   var symbolInfo = _semanticModel.GetSymbolInfo(name);
   var fieldSymbol = symbolInfo.Symbol as IFieldSymbol;
   if (fieldSymbol != null)
   {
       // Here I would like to get all the local variable names what are visible
       // in the very same scope where this field IdentifierNameSyntax under visiting resides
       // We can suppose that _semanticNodel for the Document is available.
   }
}

如何在Roslyn (Microsoft CodeAnalysis)的作用域中获得所有可见的局部变量名

调用SemanticModel.LookupSymbols() (source),然后过滤局部变量

你可能还想过滤掉在该位置之后声明的局部变量;