What is Roslyn?
Roslyn is a complete new compiler for .NET. However, it’s more than just a simple compiler. We called it earlier a “compiler as a service”. Now they call it the compiler platform.
What’s new?
Well it ain’t your run of the mill compiler. It doesn’t just take code and outputs machine code (or IL for .NET).
This compiler allows you to participate in the compilation of your software itself and tell the compiler what to do with it. Scenarios like Aspect Oriented Programming becomes relatively trivial and doesn’t require the use of plugins or post-build event.
You have a cool refactoring that you want to implement in a very specific way? Want to convert properties with certain attributes to code blocks? Just code it. Roslyn allows you to integrate your refactoring within Visual Studio directly and share it with everyone. One specific scenario would be to code company code guidelines directly within a VSIX that you deploy on every developer’s machine. This allow developers to all have the same rules as for what the company is concerned. This could definitely give an edge to a company that want to standardise code quality directly at the source.
APIs
Basically, it comes with three type of API. Features, workspaces (solution, projects, files) as well as the compiler APIs.
Features are based around refactoring and fixing code. Those are high level functionality that are tightly link to Visual Studio. Workspace API relate to code formatting, finding references, etc. They are also linked to Visual Studio. Compiler API relates to Syntax trees, emitting code, analysing flows of code… they are the most low-level API related to the compiler and are also the most interesting.
What’s coming?
Well… technically you now have access to the C# compiler with an Apache 2 licence.
Here’s what is now possible…
- Participate in the next features of C#
- Create a new language and integrate it within Roslyn
- Better/custom code analysis for your code
- Creating compiled .NET DSL for businesses
Simple scenario #1 – Creating a new refactoring
Using the Roslyn SDK, I create a new Visual C# > Roslyn > Code Refactoring.
The default template reverse the string of a type. So I press F5, create a new project, create a class and do ALT + . on that class.
I now have an additional refactoring option which brings my class “ThisTest” to “tseTsihT” with live preview. This is nothing but it’s a refactoring that you are 100% in control with which doesn’t require external tools.
I know. This refactoring is useless. If something is valuable for you, you can simply implement it or wait for someone in the community to develop it.
Simple Scenario #2 – Flagging improperly named fields
So let’s say we want to flag any field that uses the old “m_something” convention. Doing this is as simple as the following code:
1 | /// <summary> |
This require quite a bit of code. However, it is possible to regroup and refactor common operation on certain elements (fields, constructors, etc.) to reduce the amount of code.
Of course, this code is not production ready and not unit tested. Do not take it as is. It is full of bug and is not ready for any type of environment. This is only to show what is possible.
Conclusion
This of course is just the beginning. I’ve just showed you what is possible. It took me less than an hour to prepare those two examples. With more time, it could be possible to create some very complex scenarios of very high quality.
We’re living in a crazy world right now. We’re getting more and more control over the code that we write. The possibilities that are opening up when we can interact to something as low-level as the compiler are just breathtaking.
For me, it’s tools that are built for the developers, for our needs and of course… fun to use.