Microsoft & .NETAn Introduction to Microsoft Roslyn

An Introduction to Microsoft Roslyn

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Microsoft Roslyn has been released as CTP (Community Technology Preview) and can be downloaded from here. It can be installed on top of Microsoft Visual Studio 2010. Once Roslyn is installed you will find a few project templates available as shown in Fig 1.0.

Roslyn project templates
Fig 1.0: Roslyn project templates

Until now the language compilers in the .NET framework acted like a processing engine with no hook ups. It processed the input files and emited the IL output as an assembly. But with project Roslyn, Microsoft managed to de-construct the compiler into various phases and expose managed APIs for each phase. In short, Microsoft is creating the language compilers as a service. Below is the list of compiler phases, which expose the managed APIs.

  1. Language Parser
    1. Syntax API
    2. Semantic API
  2. Symbols and Metadata loader
  3. Binder
  4. IL Emitter

Current CTP of Roslyn only includes the compiler APIs for C# and VB languages.

How Roslyn Can be Used?

By having the compiler-as-a-service you would be able to invoke the .NET framework compiler APIs from your .NET applications to create new code, edit existing code and compile the same. With this feature you can perform the below tasks.

  1. Perform syntax and semantic analysis (Code analysis).
  2. Create an application to automate code refactoring.
  3. Create an application to auto correct the existing code to follow best practices.
  4. You can also use the Roslyn services to extend the Visual Studio shell.

Also Microsoft Roslyn installs a C# interactive window to the Visual Studio 2010 IDE, which can be used to execute the code snippets. Currently the C# interactive window supports some basic C# features and Microsoft is working on creating support for features like LINQ, etc.

Update an Existing Code Using Roslyn

Below is the example to load and update an existing code using Roslyn APIs. First create a new Roslyn Console Application project. You can see in the references a list having the Roslyn components Roslyn.Compilers and Roslyn.Compilers.CSharp.

namespace CodeUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the code text into the syntax tree
            SyntaxTree syntaxTree = SyntaxTree.ParseCompilationUnit(
                @"using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
 
                namespace Sample
                {
                    class Class1
                    {
                    }
                }");
            //Modify the namespace from Sample to RoslynSample
            var root = (CompilationUnitSyntax)syntaxTree.Root;
 
            //Fetch the current namespace
            var currentNamespace = (NamespaceDeclarationSyntax)root.Members[0];
            //Update the code with the new namespace
            var newNameSpace = currentNamespace.Update(currentNamespace.NamespaceKeyword, Syntax.IdentifierName("RoslynSample"), currentNamespace.OpenBraceToken,
                                                        currentNamespace.Externs, currentNamespace.Usings, currentNamespace.Members,
                                                        currentNamespace.CloseBraceToken, currentNamespace.SemicolonTokenOpt);
 
            //This updates the namespace from Sample to RoslynSample
            root = root.ReplaceNode(currentNamespace, newNameSpace);
        }
    }
}

If you run the above code and execute root.GetText() then you would get the code with the updated namespace.

These are very small pieces of work, which can be done using Roslyn. The statement below is the one that loads the text and parses it as a compilation unit.

SyntaxTree.ParseCompilationUnit

With the help of Roslyn you will be able to write your own code analysis engines, refactoring and issue correction tools.

Having seen the high level features of Roslyn, you may think of how it is different from CodeDom. CodeDom is a simple code generator utility whereas Roslyn is a set of hooks provided by the compiler to offer its services to the outside managed environment.

Roslyn is a vast area, which is still evolving and I will discuss fixing code issues using Microsoft Roslyn in a future article.

Happy reading!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories