简体   繁体   English

如何使用 Roslyn 获取范围内所有可见的局部变量名称(Microsoft CodeAnalysis)

[英]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.

相关问题 在Roslyn(Microsoft CodeAnalysis)中,如何创建布尔文字? - In Roslyn (Microsoft CodeAnalysis) how do I create a boolean literal? 为 Roslyn 分析器升级 Microsoft.CodeAnalysis.CSharp - Upgrading Microsoft.CodeAnalysis.CSharp for Roslyn analyzer 在Roslyn与Microsoft.CodeAnalysis中添加MetadataReference - Adding MetadataReference in Roslyn Vs Microsoft.CodeAnalysis 如何在Roslyn(Microsoft.CodeAnalysis)中向生成的方法添加参数? -需要确切的语法 - How to add parameters to generated method in Roslyn ( Microsoft.CodeAnalysis )? - Need exact syntax Roslyn - CodeAnalysis.MsBuild - 如何打开项目? - Roslyn - CodeAnalysis.MsBuild - How to open project? 使用Roslyn(Microsoft.CodeAnalysis)查询WebSite项目的信息 - Using Roslyn (Microsoft.CodeAnalysis) to query information of WebSite projects 如何在 memory 中编译并使用 Microsoft.CodeAnalysis 获取程序集 - How to compile in memory and get assembly using Microsoft.CodeAnalysis 如何使用 Microsoft.CodeAnalysis.Diagnostic 获取错误行 - How to get error line using Microsoft.CodeAnalysis.Diagnostic Roslyn - 如何在DiagnosticAnalyzer类中获取变量的所有引用? - Roslyn - How can I get all references of a variable at DiagnosticAnalyzer class? Roslyn - 如何使用 Roslyn 在 class 中获取属性名称和类型? - Roslyn - how to get property names and types in a class using Roslyn?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM