Chapter 27. The Roslyn Compiler

As of C# 6, the compiler is written entirely in C#, and is modular, so you can utilize its functionality in many ways besides compiling source code to an executable or library. Known as “Roslyn,” the modular compiler makes it easier to write static code analysis and refactoring tools, editors with syntax highlighting and code completion, and Visual Studio plug-ins that understand C# code.

The Roslyn libraries can be downloaded from NuGet, and there are packages for both C# and VB. As both languages share some architecture, there are common dependencies. The NuGet package ID for the C# compiler libraries is Microsoft.CodeAnalysis.CSharp.

The source code for Roslyn is publicly available under the Apache 2 open source license. This opens up further possibilities, including morphing C# into a custom or domain-specific language. The source code is available on GitHub, at https://github.com/dotnet/roslyn.

The GitHub site also hosts documentation, examples, and walkthroughs that demonstrate code analysis and refactoring.

Warning

The .NET Framework does not ship with the Roslyn assemblies, and its version of csc.exe invokes the old C# 5 compiler. Installing Visual Studio 2017 remaps csc.exe to the C# 7 (Roslyn) compiler.

Without Visual Studio 2017, you can still programmatically invoke the compiler (and its services), if you download and reference the Roslyn assemblies. But the csc.exe tool that ships with the .NET Framework will remain pointed at C# 5 ...

Get C# 7.0 in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.