Home > Articles > Programming > C#

Automate Your Development: Build Code Diagnostics and Code Fixes with the .NET Compiler Platform APIs

  • Print
  • + Share This
An important feature of the API framework in the Visual Studio 2015 compiler is the capability to create your own diagnostic, code fix, and refactoring projects. Bill Wagner, author of Effective C#: 50 Specific Ways to Improve Your C#, Second Edition, shows how you can use these APIs to automate everyday tasks such as finding code that lacks enclosing braces - and then adding them automatically.
From the author of

Visual Studio 2015 introduces a new compiler, including an API framework that enables you to write extensions that can spot and fix common coding issues. You get to use the same APIs used by the Visual Studio team (or the ReSharper or CodeRush teams) to create those extensions.

These APIs and extensions are bringing new features to Visual Studio and your favorite extensions. You can see some of this work already in the public Visual Studio 2015 preview. New refactorings are available, along with new suggestions as you work. You'll continue to see new investments that will make you more productive.

More significantly, you can create your own diagnostic, code fix, and refactoring projects, with which you can create projects that enforce your own coding practices or automate common tasks. In this article, I'll provide a tour of the API structure for code-fix and diagnostic projects. I'll show how you can analyze code to find possible mistakes, and you'll learn how to write tools to fix those mistakes. For example, I'll build a code-fix project to find if or else blocks that are not surrounded by braces, and then add the missing braces to that code.

Projects and Tools

To run these projects, start by downloading the Visual Studio 2015 public preview. You'll also need the Visual Studio 2015 Preview SDK, available from the same web page. If you've used earlier previews, you may remember that the earlier Roslyn community technology previews (CTPs) would install as an extension to Visual Studio 2013. Those extensions are not being updated for the latest API changes. Now that the Visual Studio 2015 Preview is available, you'll use that instead. It installs side by side with Visual Studio 2013, and it will be updated as the team nears release.

Finally, get the .NET Compiler Platform Syntax Visualizer extension. If you've used the previous CTPs, you may have seen this extension under the name "Roslyn Syntax Visualizer." The updated version uses the official product name. It's indispensable in seeing the syntactic and semantic trees formed when the code in a target project is parsed. Throughout this article, I'll discuss those structures, and you'll learn how you can use the visualizer to see exactly how the code relates to the structures we use to interpret and manipulate the code.

Once you've installed these extensions into your Visual Studio 2015 environment, you're ready to create a project. Three project types leverage the new compiler APIs:

  • Compiler Platform Console Application
  • Code Refactoring
  • Diagnostic with Code Fix

All three use the same platform APIs. I'll concentrate on the Diagnostic with Code Fix project type in this article (see Figure 1).

Figure 1 You can create three kinds of projects: console packages, refactorings, and diagnostics with code fixes.

  • + Share This
  • 🔖 Save To Your Account