简体   繁体   English

如何使用Roslyn代码诊断识别和分析局部变量和参数?

[英]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()有一些stringint类型的局部变量。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM