问题
(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