We often think about a compiler as a black box which produces an output from an input. Compilers were generally written in C++ because of speed, custom memory, file, and thread management needs. For example, the C# compiler takes .cs
files in input and generates .dll
files in output.
Now the new Microsoft compiler written in C# and VB, Roslyn, is not just a compiler; it's a .NET compiler platform that we can extend.
Learning to use all Roslyn APIs could be the subject of an entire book. In this recipe, we will just manipulate one of these APIs: the Syntax Trees API. We will create a Syntax Tree and we will analyze it.
- Let's download the SDK and extend the compiler, installing the VS extension,
.NET Compiler Platform SDK
, by going through theTools | Extensions and Updates
menu, searching for Roslyn SDK:
- We can now download the .NET Compiler Platform SDK templates as a
.vsix
file, which gives us all the available project templates to create an analyzer...