Microsoft announced Community Technology Preview (CTP) of Roslyn project during BUILD 2011. I am really excited about the Roslyn project (and I will tell you why soon…).

Roslyn? Isn’t it name of a city?

Well you are right and many of Microsoft product code names are now based on city/town names mainly due to copyright issue. If you don’t have idea and you love to dig in languages/compilers, it’s a MUST SEE for you.

The concept is simple that traditionally compilers are kind of black box thing where we provide source files as input and it provide assemblies/DLLs/exe as output. The Roslyn project is all about opening the black box of compiler and allowing us to get internal language object model and things such as syntax tree etc. through Compiler APIs. The objective of the project is to rewrite the VB and C# compilers and language services in managed code. See slide below from Anders’ talk at BUILD 2011.

The Roslyn Project

Interested? Check out http://msdn.com/roslyn for project overview document and to download the available bits. For further introduction, please read Introducing the Microsoft “Roslyn” CTP on Visual Studio blog.

Roslyn APIs

Roslyn APIs exposes four collection of APIs. You can read details of each API in project overview document (from where the image below is taken).

Roslyn APIs Collection

The Services API have dependency over Visual Studio SDK and contains Visual Studio IDE features, such as intellisense, code refactoring and formatting etc.

Before reading on, if you haven’t installed Roslyn binaries, go ahead download them and skim through overview document.

Exciting Part?

If you have been kind of student, who enjoyed compiler construction course(s) during undergrad and really enjoy in going deep into mechanics, then you would definitely love exploring Roslyn APIs. This is because the rewriting of compiler and exposed APIs would allow you to understand the syntactical and semantic model of your code.

Roslyn Core APIs

With this you can not only generate code on fly but also REPL (read-eval-print-loop) operations can be done. Plus you can visualize/parse your existing code and see what syntax tree it has generated. For example, consider the following code snippet using Roslyn API:

SyntaxTree tree = SyntaxTree.ParseCompilationUnit(
@"using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{ Console.WriteLine(""Hello, World!""); }
}
}"
);

Syntax Visualizer

If you traverse/print tree or use Roslyn Syntax Visualizer control, you would get to know the underlying syntax tree structure and its components. Using the above code, the syntax tree has parsed the code and decomposed it into tokens, nodes, and trivia. The debugger visualizer (see image on left) will help you understand the syntax tree generated. This debugger is of great aid if you are developing any IDE extension using Roslyn API.

Please note that syntax visualizer control is available with samples, along with source code, in Samples folder created when you downloaded Roslyn bits. Currently there are some known issues with this control and workaround (code modification) has been explained at Roslyn forum “Known Limitations and Unimplemented Language Features”.

Another fun point is Workspace API which allows you to programmatically do stuff with your solution, projects, assemblies and documents. As a very simple example, consider the following code snippet:

IWorkspace workspace = Workspace.LoadSolution(@"<%PROVIDE PATH OF .SLN FILE");
ISolution solution = workspace.CurrentSolution;

foreach (IProject project in solution.Projects)
{
Debug.WriteLine(project.DisplayName);
foreach (IDocument document in project.Documents)
{
Debug.WriteLine(document.DisplayName);
}
}

And if you are interested to create visual studio extension for developers productivity, Roslyn Service APIs are going to provide you breakthrough. Try exploring Service APIs project using “Roslyn Installed Templates”. Surely, you can VS extensions directly using SDK but with the power of Roslyn, you can get into the code tree and provide intelligent refactoring and productivity aids to developer.

Roslyn Templates 

Do I need Roslyn?

You need to explore Roslyn APIs,

  • if you do a lot of heavy lifting with Reflection in the .NET Framework. At my previous employer, when we used to develop Form Designer for BPM Suite, much percent of our code was based on Reflection due to dynamic nature of the application
  • Already using Visual Studio SDK
  • Interested to understand compiler services
  • Interested to create Visual Studio extensions

But there is much more in Roslyn and I would try to share my experience of whatever I experiment with Roslyn APIs in spare time. If you are working on Roslyn, please do share your experience in comments below.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *