简体   繁体   中英

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

(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.
   }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM