- Roslyn Cookbook
- Manish Vasani
- 326字
- 2021-07-15 17:07:29
How to do it...
- Start Visual Studio and click on File | New | Project.
- 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:

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

- In the subsequent File Download dialog, click on Open.

- Click Install on the next VSIX Installer dialog and End Tasks on the subsequent prompt to install the SDK:

- Start a new instance of Visual Studio and click on File | New | Project... to get the New Project dialog.
- 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.

- You should now have an analyzers solution with 3 projects: CSharpAnalyzers (Portable), CSharpAnalyzers.Test , and CSharpAnalyzer.Vsix:

- 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:

- 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.
- In the new Visual Studio instance, create a new C# class library project, say ClassLibrary.
- 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.
- We should now see the analyzer diagnostic in the error list and a squiggle in the editor:

- Edit the name of the class from Class1 to CLASS1.
- 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.
推薦閱讀
- SQL學習指南(第3版)
- 數據結構簡明教程(第2版)微課版
- MySQL數據庫管理與開發(慕課版)
- MySQL數據庫管理與開發實踐教程 (清華電腦學堂)
- Responsive Web Design by Example
- Building Serverless Applications with Python
- Getting Started with Gulp
- Scala for Machine Learning(Second Edition)
- Unity&VR游戲美術設計實戰
- Kubernetes進階實戰
- Scala Functional Programming Patterns
- 深入實踐DDD:以DSL驅動復雜軟件開發
- 算法圖解
- Spring MVC Cookbook
- 編譯原理學習與實踐指導