How to get all visible local variable names within a scope with Roslyn (Microsoft CodeAnalysis)

◇◆丶佛笑我妖孽 提交于 2019-12-07 11:03:04

问题


(Please note: This is not about run-time reflection/metainfo)

I am writing a concrete implementation of Roslyn CSharpSyntaxVisitor

When implementing the 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.
   }
}

回答1:


Call SemanticModel.LookupSymbols(), then filter for local variables.

You may also want to filter out locals declared after that location; see this code.



来源:https://stackoverflow.com/questions/23539872/how-to-get-all-visible-local-variable-names-within-a-scope-with-roslyn-microsof

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!