问题
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