How can I identify and analyze local variables and parameters with Roslyn code diagnostics?

∥☆過路亽.° 提交于 2019-12-12 15:22:12

问题


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().

It's likely RegisterSymbolAction() as per the sample Diagnostic with Code Fix (NuGet + VSIX) in the Roslyn SDK Project Templates.vsix.

I'm debugging using a simple console app whose Main() has a handful of local variables of type string and int.

I've tried these two, but neither will trigger any variable to be analyzed in their respective AnalyzeSymbol() callback methods. 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?

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.


回答1:


Unfortunately RegisterSymbolAction currently only work methods and above. For locals, and parameters you need to use RegisterSyntaxNodeAction. We hope to address this in a later build.



来源:https://stackoverflow.com/questions/28004656/how-can-i-identify-and-analyze-local-variables-and-parameters-with-roslyn-code-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!