Posts

Showing posts from October, 2011

Playing With Roslyn

Image
As a tools guy, I've been fascinated with Roslyn ever since Microsoft previewed it. It looks like it provides lots of power for tools to consume. Now that the first CTP is out, I spent some time over the weekend playing with it. The first thing it gives you access to is parsing source code, giving you an object model you can play with, and then letting you spit the modified source code back out. Reading source code in is easy: var source_text = File.ReadAllText ("input.txt"); var tree = SyntaxTree.ParseCompilationUnit(source_text); var root = (CompilationUnitSyntax)tree.Root; Now we have an object model we can play with. We can search through the model for specific tokens and replace them with new ones, like this: // Replace "Hello World!" with "Goodbye World!" var output = root.GetFirstToken (p => p.Kind == SyntaxKind.StringLiteralToken); var new_output = Syntax.StringLiteralToken ("\"Goodbye World!\"", "Goodbye