Categories

Meta

Month: August 2013

(Blog post and mini tutorial)

So Microsoft is working on this amazing new compiler for C# and VB.NET code named “Rosyln” which will make you, your dog, your parents, and the world happy. Why? Is it faster or stronger or something? Well, yes but that’s pretty normal for new software. The news here is compiler APIs. Let me say it again…

Compiler APIs.

Okay, let that sink in. What it is useful for? Everything from re-factoring code on the fly to powering the new intellisense in Visual Studio 2013 to whatever.

Show me

Oh, so you want an example, eh? Here’s your example: Suppose you need to re-factor some code and you’re using a regular expression to parse and replace code fragments. But what if you run into some newer code that uses a “var” for example. What will you do? You could come up with a massively complex way of handling that scenario oryou could use Roslyn. Because Roslyn exposes the compiler as an API you have access to the semantic tree and all the metadata that tree exposes.

Yes it’s a bit complicated but really look at it and you’ll see how amazingly simple it really is. This needs not only .NET 4.5 (Visual Studio 2012 or higher) but also the Roslyn CTP which you can get from NuGet very easily but running this in your NuGet Package Manager Console (you do have that turned on, right?)

Now compile, run, and examine the contents of result. You will find the var has been replaced by “String” which is what it should be. If you change the value of the “helloWorld” var in the sample code to a 1, and re-run you should see the var replaced by an Int32.