[英]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我正在编写 Roslyn CSharpSyntaxVisitor 的具体实现
When implementing the VisitIdentifierName实施 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.调用
SemanticModel.LookupSymbols()
( source ),然后过滤局部变量。
You may also want to filter out locals declared after that location;您可能还想过滤掉在该位置之后声明的本地人; see this code .
看到这个代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.