|The Roslyn project is the Microsoft implementation of C# and VB compilers-as-a-service. Roslyn provides a transparent view into the inner workings of the compiler, including syntax tree inspection and modification.||
original image, license: CC BY-NC 2.0
Some of the scenarios enabled by Roslyn are the following:
Refactoring. Refactoring tools no longer need to parse the original source code and then reconstruct it manually. Instead, a set of rewriting APIs is available on the syntax tree level, making possible classical refactorings—“make readonly”, “extract method”, “extract interface”—as well as incredible new things yet to be invented.
Code analysis. Performing static code analysis—especially when data flow and control flow are involved—is an extremely difficult task without the syntactic and semantic compiler engines. It becomes possible to answer questions like “is this local variable returned from the method?”, “is this parameter assigned in this specific block under that specific condition?”, “is this method invoked with a compile-time constant expression?”.
“Quick Fix”. Visual Studio extensions that run in the background can detect common source code issues and suggest a fix.
REPL (Read-Eval-Print Loop). To test code quickly against a given API or to invent a new API and then try it out is easier than ever with the C# Interactive Window in Visual Studio. It is more powerful than the Immediate Window, because you can use the full power of the language—including lambdas and query comprehensions—and because you can define new types or methods and immediately use them.
Scripting. Roslyn ships with a command-line tool called rcsi.exe that evaluates .csx files—script files written in C# that are clear of the “class Program … void Main” clutter and focus on exactly the task at hand. I’ll go as far as saying that this is serious competition for PowerShell!
Embedded compiler. It is now easier to embed the C# compiler in an application and provide an ambient context (host object model) implicitly to the compiled code. Think DSLs and rule engines—taking advantages of the full power of C#.
In the next couple of posts we’ll perform some experimentation with the Roslyn APIs. If you are looking for more, right away, then go ahead and read the “Getting Started” documents and the samples that ship with the CTP.