Adil's Tech Weblog

Why am I excited about Roslyn Project

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.

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).

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.

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:

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:

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.

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

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.