Roslyn Compiler in Visual Studio 2015

Introduction

The Roslyn team started by re-implementing the compiler in C#. That means that developers can clone the company's compiler for C# projects and add their own features, as well as suggest new features for Microsoft to implement in the compiler itself. Roslyn is open source! The .Net Compiler Platform, also known as "Roslyn", is the most awaited project of Microsoft. The C# and VB teams got together and rewrote the compilers and language services in managed code, replacing a horrible mess of C++ code (with some managed code) that had mutated over the past ten years into a complex and difficult-to-modify code base.

The Roslyn compiler fetches information regarding the entire source code like what and which type of elements are present in the code, what the real meanings of those elements are, how they are communicated and related to the each other and the IL emitting (executable code). The Visual Studio templates available for both Visual Basic and C# installed with the Community Technology Preview are Console Applications, Code Issues, Code Refactoring and Completion Provider.

Roslyn allows us to easily write C# (or VB) code that parses a source file and rewrites the code. This makes it easy to build many tools to analyze or modify source code. Roslyn exposes a simple extensibility model that lets you create your own warnings or refactorings that integrate with Visual Studio. Thanks to the newly accessible semantic information, Roslyn includes a number of new Refactoring and has vastly improved the existing ones (especially Rename).

work flow of Roslyn compiler

Installation Procedure for Roslyn compiler

To properly install the Roslyn compiler in ASP.NET we need to do the following two things:

  • Install the .NET Compiler Platform (“Roslyn”) End User Preview on your developer machine (where you use Visual Studio or build your application's project file).

  • Install the new Code DOM Providers for .NET Compiler Platform (“Roslyn”) NuGet package into your ASP.NET application. In the Package Manager Console in Visual Studio type: install-package.

The Roslyn API(s) are available as NuGet Packages, so you can install it very easily using the Package Manager.

    First of all you need the Roslyn Libraries available as a NuGet Package that can be installed using the Package Manager in Visual Studio. For installing the Roslyn libraries, just open the Package Manager console and type in the following command:
PM> Install-Package Roslyn.Compilers.Common

Once the installation is successful, create a sample console application or a Web Application as needed and add the reference to the two libraries, that you got from the preceding installation. The two libraries are:

  • Roslyn. Compilers
  • Roslyn.Compilers.CSharp

Why the Roslyn compiler is in ASP.NET

Enabling the new Roslyn compilers in your ASP.NET application will result in the following two main benefits:

  • Support for new language features.

  • Potentially improve application startup/pre-compilation time (in other words reduce the time of program execution). The second should be particularly helpful for customers with very large, complex ASP.NET applications.

  • In our testing of a suitably large and complex application, the runtime compilation cost at startup/pre-compilation dropped from 15 minutes to 70 seconds after enabling the new Code DOM.

Roslyn Layers

Roslyn mainly has the following four API layers:

  • Scripting APIs: Provides a runtime execution context for C# and VB.NET. Now we can use C#/VB.NET in your own applications as a scripting language.
  • Compiler APIs: for accessing the syntax and semantic model of our code.
  • Workspace APIs: Provides an object model to aggregate the code model across projects in a solution. Mainly for code analysis and refactoring around IDEs like VisualStudio, though the APIs are not dependent on Visual Studio.
  • Services APIs: Provides a layer on top of the Visual Studio SDK for features like IntelliSence, code formatting and so on.

Roslyn API

Security performances

The Roslyn compiler can only execute only that programming code that the code (application) could already execute and push it on the Visual Studio .NET Framework with the similar security aspects and issues. In that way, it does not do any new security aspects that are not already present. But the main advantage of Roslyn is that it makes the code easier than code that was written before. Dynamically executing code at run time has always been problematic. The potential exists for an application to execute malicious code in a way that may not be easily detected by current virus scanners.

How to use the Roslyn compiler

  • First install the Roslyn using NuGet.

  • Open Visual Studio .Net.

  • Make a new project.

  • Click on Solution Explorer and add the two web references, one is Roslyn.Compilers and the second is Roslyn.Compilers.CSharp.

  • Now you are able to make the application.

  • When you open Visual Studio then in the template field you watch a template named by Roslyn.

  • Select this template folder and create a new Console Application from within it. This will give you a new console app with the references that you need to get started with Roslyn.

  • Now our app is ready. Here we need to add a new class to the project. This class will be the one that we manipulate with Roslyn.

  • Now we can write the code depending on our needs and compile it. This type of application is called a Roslyn based application.

Design principals

Data structures created by Roslyn compilers are present in the immutable format and can't be changed during calling application. This is forcefully necessary to keep from crashing the compiler. Also, the initial problems introduced by allowing changes in the compile process are not allowed. The syntax tree provided to the Roslyn user is a read-only snapshot of the compiler's current understanding of the code. Rebuilding a full syntax tree at each code change would be not allowed from a performance and memory consumption view. The main challenge for Roslyn was how to immediately surface an immutable data collection from a core syntax tree that was modified live as well as code changed by the developer.

How Roslyn influences the future development of the C# compilers

For influencing the compiler in the future we use the Delphi IDE. The Delphi IDE is a very important IDE. The Delphi IDE was programmer-friendly and has a user friendly environment that included many nice touches, it was very popular because the developers wanted those features in their code. We know that any software in the C# and VB compilers are not fully bug-free. The Roslyn team worked with the dilemma to find errors in the new code or fix them. Sometime, the decision was good to fix the bug even if it can create the problem in the code but sometimes it played a very important role for the future prospective to make the software more reliable. By verifying our solution with Roslyn we can avoid errors in the future and are able to fix it without hurrying.

Summary

The Roslyn compiler exposes a set of Compiler APIs and Services APIs that provide rich information about our source code. It provides many functionalities, like IntelliSence, reformatting and many more through which our code becomes more reliable and useful for the user. It also provides language interoperability and multiple language support. The .NET Compiler Platform (“Roslyn”) exposes a set of Compiler APIs and workspace APIs that provide rich information about your source code and that has full fidelity with the C# and Visual Basic languages. The transition to compilers as a platform dramatically lowers the barrier to entry for creating code-focused tools and applications. It creates many opportunities for innovation in areas such as meta-programming, code generation and transformation, interactive use of the C# and VB languages and the embedding of C# and VB in domain specific languages.


Similar Articles