How it works...

Code block analyzers register code block actions to analyze executable code blocks in the compilation. You can register either a stateless CodeBlockAction or a stateful CodeBlockStartAction with nested actions to analyze syntax nodes within a code block. Our analyzer registers a CodeBlockStartAction to perform stateful analysis.

 context.RegisterCodeBlockStartAction<SyntaxKind>(startCodeBlockContext => {  ... }

Analysis begins with a couple of early bail out checks: we are only interested in analyzing executable code within a method body and methods that have at least one parameter.

  // We only care about method bodies.  if (startCodeBlockContext.OwningSymbol.Kind != SymbolKind.Method)  {    return;  } // We only care about methods ...

Get Roslyn Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.