[英]How can I identify and analyze local variables and parameters with Roslyn code diagnostics?
I'm confused which method to use on the AnalysisContext context
object to have every function/method's local variables anazlyed: either RegisterSymbolAction()
or RegisterSyntaxNodeAction()
. 我很困惑在
AnalysisContext context
对象上使用哪种方法来使每个函数/方法的局部变量都成为anazlyed: RegisterSymbolAction()
或RegisterSyntaxNodeAction()
。
It's likely RegisterSymbolAction()
as per the sample Diagnostic with Code Fix (NuGet + VSIX) in the Roslyn SDK Project Templates.vsix . 根据Roslyn SDK Project Templates.vsix中的示例Diagnostic with Code Fix(NuGet + VSIX),它可能是
RegisterSymbolAction()
。
I'm debugging using a simple console app whose Main()
has a handful of local variables of type string
and int
. 我正在调试使用一个简单的控制台应用程序,其
Main()
有一些string
和int
类型的局部变量。
I've tried these two, but neither will trigger any variable to be analyzed in their respective AnalyzeSymbol()
callback methods. 我已经尝试过这两个,但是它们都不会触发任何变量在各自的
AnalyzeSymbol()
回调方法中进行分析。 Breakpoint within each of those callback methods don't get hit for the local variables. 每个回调方法中的断点都不会受到局部变量的影响。
How can I get local variables to be analyzed in the callback method AnalyzeSymbol()
and/or what am I doing wrong? 如何在回调方法
AnalyzeSymbol()
分析局部变量和/或我做错了什么?
var symbolsToActOn = new[] { SymbolKind.Local, SymbolKind.Parameter, SymbolKind.Field };
context.RegisterSymbolAction(AnalyzeSymbol, symbolsToActOn);
or 要么
var syntaxTypes = new[] { SyntaxKind.IdentifierName, SyntaxKind.IdentifierToken, SyntaxKind.Parameter };
context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode, syntaxTypes);
My demo project is on GitHub for a closer look, and the specific piece is in DiagnosticAnalyzer.cs . 我的演示项目在GitHub上进行了仔细研究,具体部分在DiagnosticAnalyzer.cs中 。
Unfortunately RegisterSymbolAction
currently only work methods and above. 不幸的是
RegisterSymbolAction
目前只有工作方法及以上。 For locals, and parameters you need to use RegisterSyntaxNodeAction
. 对于locals和参数,您需要使用
RegisterSyntaxNodeAction
。 We hope to address this in a later build. 我们希望在以后的版本中解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.