【发布时间】:2019-12-18 16:57:48
【问题描述】:
(请注意:这与运行时反射/元信息无关)
我正在编写 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.
}
}
【问题讨论】:
-
我认为这是不可能的。相反,您可以做的是定义匿名变量,该变量将包含您拥有的所有变量。然后,您可以使用反射浏览该实例的所有属性。例如var locals = new { symbolInfo, fieldSymbol,... }。然后使用 locals.GetType().GetProperties()
-
也许我错过了什么,但您的回答似乎不是关于 Roslyn SyntaxTree 而是一般反射
-
是的,很抱歉。没关系