简体   繁体   中英

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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