What is Roslyn?
The “Roslyn” project is a Microsoft initiative for an open-source compiler that also provides a rich set of analysis on the code that is being compiled.
A brief history of this project
The initial CTP for Roslyn was on October 2011 so this project has been around for a few years already.
In April 2014, Microsoft made it an open source project and released a preview of the integration with VS2013. At that time, the project transferred to the responsiblity of the newly founded .NET Foundation.
Roslyn’s first RTM release was with Visual Studio 2015.
Roslyn currently only supports VB and C# and the source code is now on GitHub.
You can find the source code here:
Why do we need this? How does this help us write better code or improve productivity?
In the past compiling code was done by a black box. We submitted code to the compiler and it generated the code that we would execute. We could only understood the decisions made by the compiler (during the compile process) by examining the code that was generated by the compiler.
“Roslyn” is a compiler that allows to see what is going on during (and interact with) the compile process.
This capabilities provides us some interesting and powerful capabilities.
For example, we can :
(a) Compile code and analyze the assemblies on the fly as they are compiled.
We can use Roslyn to compile an entire solution and then determine (based the assemblies are each project references during the build process) which assemblies are being referenced by each project. This allows to identify and report the assemblies needed in a given solution, and by each project within a solution.
(b) Make changes to the code hierarchy of the original source code in order to generate new code.
Roslyn does not provide an architecture that allows you to make changes to the source code as it goes through the compiler pipeline – but you can use Roslyn to analyze the source code and generate a different source code by rewriting the syntax trees, changing references, etc.. The new source code can then be compiled for the desired final output.
(c) Enforcing coding standards is made a lot easier.
We can create Diagnostic Analyzers and Code Fixes which can be added into a given solution or project. These rules and fixes become part of the source code of the solution/project and can be enforced by the compiler at every stage of the build. We can create our own analyzers to enforce custom coding standards within our organization. We can even perform code analysis on solutions that use our Nuget packages in order to make sure that they are using our APIs correctly.
To better understand Roslyn, install the Syntax Visualizer in VS2015.
The Syntax Visualizer helps you identify patterns in the syntax trees. It will help you understand how your code is being interpreted by Roslyn and may help you identify any problem in the code.
To install this tool, simply open VS2015 and install the following VS extension:
.NET Compiler Platform SDK
Current version: 18.104.22.168618
Open Visual Studio 2015 > Tools > Extensions and Updates >
Select the .NET Compiler Platform SDK
Once installed go to View > Other Windows > Syntax Visualizer.
This will open a new Window with 2 panes:
Top = the Syntax tree;
Bottom = the properties of the currently selected item.
These 2 panes will initially be blank – but if open any C# file and put your cursor on any C# code – then the 2 panes will show you exactly the breakdown of your currently selected C# code.
(See image below. It shows how Roslyn is interpreting my simple console app – Main method.)
It gets really interesting when you see your code hierarchy.
This shows me a complete breakdown of the Syntax elements that make up my Main() method.
This provides me a complete (and in-depth) presentation of how my code is being interpreted by the compiler.
Using this information I can then determine how to create diagnostic visualizers and code fixes that I can add to my source code in order to enforce my coding standards.
If you wish to learn more about how to use Roslyn as a code analyzer and how to create custom Analyzers and Code fixes, then use the following links:
How to write a Live Code Analyzer
How to create a code fix