How to do it...

  1. Start Visual Studio and click on File | New | Project.
  2. Search for Analyzer templates in the textbox at the top right corner of the New Project dialog, select Download the .NET Compiler Platform SDK, and click on OK:
  1. The new project will have an index.html file opened by default. Click on Download .NET Compiler Platform SDK Templates >> button to install the analyzer SDK templates.
  1. In the subsequent File Download dialog, click on Open.
  1. Click Install on the next VSIX Installer dialog and End Tasks on the subsequent prompt to install the SDK:
  1. Start a new instance of Visual Studio and click on File | New | Project... to get the New Project dialog.
  2. Change the project target framework combo box to .NET Framework 4.6 (or above). Under Visual C# | Extensibility, choose Analyzer with Code Fix (NuGet + VSIX), name your project CSharpAnalyzers, and click on OK.

  1. You should now have an analyzers solution with 3 projects: CSharpAnalyzers (Portable), CSharpAnalyzers.Test , and CSharpAnalyzer.Vsix:
  1. Open source file DiagnosticAnalyzer.cs in CSharpAnalyzers project and set breakpoints (press F9) at the start of the Initialize and AnalyzeSymbol methods, as shown here:
  1. Set CSharpAnalyzers.Vsix as the start-up project and click on F5 to build the analyzer and start debugging a new instance of Visual Studio with the analyzer enabled.
  2. In the new Visual Studio instance, create a new C# class library project, say ClassLibrary.
  3. Verify that we hit both the preceding breakpoints in our analyzer code in the first VS instance. You can step through the analyzer code using F10 or click on F5 to continue debugging.

 

  1. We should now see the analyzer diagnostic in the error list and a squiggle in the editor:
  1. Edit the name of the class from Class1 to CLASS1.
  2. We should hit the breakpoint in the AnalyzeSymbol method again. Continue debugging with F5 and the diagnostic and squiggle should go away immediately, demonstrating the powerful live and extensible analysis.