如何使用 Roslyn 获取范围内所有可见的局部变量名称(Microsoft CodeAnalysis) [英] How to get all visible local variable names within a scope with Roslyn (Microsoft CodeAnalysis)
本文介绍了如何使用 Roslyn 获取范围内所有可见的局部变量名称(Microsoft CodeAnalysis)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(请注意:这与运行时反射/元信息无关)
(Please note: This is not about run-time reflection/metainfo)
我正在编写 Roslyn CSharpSyntaxVisitor 的具体实现
I am writing a concrete implementation of Roslyn CSharpSyntaxVisitor
实施 VisitIdentifierName 时
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.
}
}
推荐答案
调用 SemanticModel.LookupSymbols()
(source),然后过滤局部变量.
Call SemanticModel.LookupSymbols()
(source), then filter for local variables.
您可能还想过滤掉在该位置之后声明的本地人;见 noref="n2422"/a>.
You may also want to filter out locals declared after that location; see this code.
这篇关于如何使用 Roslyn 获取范围内所有可见的局部变量名称(Microsoft CodeAnalysis)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文