Introducing the Microsoft “Roslyn” CTP

Introducing the Microsoft “Roslyn” CTP

Rate This
  • Comments 17

Today we are releasing the first Community Technology Preview of the Roslyn Project!

What is Roslyn?

In the past, our compilers have acted as black boxes – you put source text in and out the other end comes an assembly. All of that rich knowledge and information that the compiler produces is thrown away and unavailable for anyone else to use.

As Soma mentions in his blog, a part of the Visual Studio languages team is working on a project called Roslyn with a goal to rewrite the C# and VB compilers and language services in managed code. With a clean, modern, managed codebase our team can be more productive, innovate faster, and deliver more features sooner and with better quality.

More importantly, we are opening up the C# and Visual Basic compilers and exposing all that rich information and code analysis to be available for your use. We expose a public API surface and provide extension points in the C# and VB language services.

This opens up new opportunities for VS extenders to write powerful refactorings and language analysis tools, as well as allow anyone to incorporate our parsers, semantic engines, code generators and scripting in their own applications.

Download the October 2011 CTP

The CTP and supporting materials can be downloaded from:

http://msdn.com/roslyn

The main goal of this early preview is to gather feedback on the API design and to introduce the C# Interactive window (also known as REPL, or Read-Eval-Print-Loop).

This first CTP is intended for preview-use only and does not allow redistribution of the Roslyn components or allow use in a production environment.

The CTP installs on Visual Studio 2010 SP1. It also requires the Visual Studio 2010 SP1 SDK.

Getting Started

After the installation succeeds, the best place to start is to open Start Menu -> Microsoft Codename Roslyn CTP -> Getting Started.

To get started, the “Roslyn Project Overview” document gives a look at the compiler API – how to work with syntax and semantics of your program. Several walkthrough documents are also included to provide a deep dive into various aspects of the Roslyn APIs.

The CTP ships with quite a few samples for Visual Studio Extensions, compiler API, code issues, refactorings and so on. Most of the samples are provided for both C# and Visual Basic. You can open the sample source code from the Getting Started page.

We also install several new project templates available in the New Project dialog:

ProjectTemplatesVB

ProjectTemplatesCSharp

These templates will help you to get started on a new Visual Studio extension that uses Roslyn.

Reference Assemblies

References

The Roslyn assemblies are also installed in the GAC. Switch to the Full Profile (instead of the Client Profile) to be able to also reference the Services assemblies (which contain the IDE support).

C# Interactive window

InteractiveWindow

You can invoke the C# Interactive window from View -> Other Windows -> C# Interactive Window. The Interactive window is powered by the new C# language service. The architecture of Roslyn is flexible enough to allow many of the IDE features such as IntelliSense and refactorings to work the same in a normal editor and in the Interactive window.

At this time, the Interactive window is only available for C#. We’re working hard on providing the VB Interactive at a future time.

C# Script File (.csx) Editing Support

The CTP introduces a concept of a C# Script File. You can create a .csx file through File -> New File (or also use any other editor such as notepad):

NewCSharpScriptFile

CSXScriptEditor

You can run scripts using the new rcsi.exe, which installs into %ProgramFiles(x86)%\Microsoft Codename Roslyn CTP\Binaries\rcsi.exe. You can add rcsi.exe to the path and then type rcsi <scriptfilename>.csx.

You can also copy chunks of code from a script file and send them to the C# Interactive Window (using the right-click context menu or a keyboard shortcut).

The editor for the script files is also powered by the new language services. Hence it is important to keep in mind that .csx scripts will only support the part of the language already implemented in the Roslyn compilers. For more details, see the "Introduction to Scripting" walkthrough.

Quick sample of the Roslyn API

Here’s a sample of compiling and executing a small program using the Roslyn API.

using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;

...

var text = @"class Calc { public static object Eval() { return 42; } }";
 
var tree = SyntaxTree.ParseCompilationUnit(text);
var compilation = Compilation.Create(
    "calc.dll",
    options: new CompilationOptions(assemblyKind: AssemblyKind.DynamicallyLinkedLibrary),
    syntaxTrees: new[] { tree },
    references: new[] { new AssemblyFileReference(typeof(object).Assembly.Location) });
 
Assembly compiledAssembly;
using (var stream = new MemoryStream())
{
    EmitResult compileResult = compilation.Emit(stream);
    compiledAssembly = Assembly.Load(stream.GetBuffer());
}
 
Type calc = compiledAssembly.GetType("Calc");
MethodInfo eval = calc.GetMethod("Eval");
string answer = eval.Invoke(null, null).ToString();
 
Assert.AreEqual("42", answer);

Note: At this stage, only a subset of the language features has been implemented in the current CTP. We’re moving forward at a fast pace, but features such as Linq query expressions, attributes, events, dynamic, async are not yet implemented. To see a full list of non-implemented language features, see the Roslyn forums.

Although not all the language features are supported, the shape of the public API is mostly complete, so we encourage you to write extensions and tools against the Syntax, Symbols, and Flow and Region Analysis APIs.

We’re very excited to get an early preview of this technology in your hands and we welcome your feedback, ideas and suggestions. Use the forums to ask questions and provide feedback, Microsoft Connect to log bugs and suggestions, and use the #RoslynCTP hashtag on Twitter.

 

Thanks,

Kirill Osenkov

QA (Roslyn Services Team)

Blog: http://blogs.msdn.com/b/kirillosenkov

Twitter: @KirillOsenkov

Leave a Comment
  • Please add 2 and 8 and type the answer here:
  • Post
  • Hi Kirill and Team!

    I just took a quick look at the Roslyn API example above and it looks great.  As someone who often has to write software engineering tools as extensions for Visual Studio this API seems like it has the potential to save me lots of time.  However, I notice that Roslyn currently only supports VB and C#.  I was just curious if it is within the scope of the Roslyn project to support C and C++ at some point.  

    Thanks,

    Dave

  • So using Roslyn could we write a compiler extension that allows us to add a [NotifyChanged] attribute to an automatic property that then gets turned into a full property that raises the PropertyChanged event at compile time? That would be pretty sweet!

  • Hi Dave,

    Thanks for the comment! Roslyn is for VB and C# only.

    Enjoy!

    Kirill

  • Jonathan: transforming a syntax tree to replace [NotifyChanged] with the property implementation is definitely possible. However we haven't thought about building this kind of extensibility into the actual compiler yet (that would be something like PreSharp as opposed to the currently existing PostSharp project). Right now we just want to focus on building a high quality compiler, we'll have to think carefully about metaprogramming sometime later in the future.

  • This all very, very cool stuff - I love the C# Interactive window. Was that just a really nice side-effect of the new compiler, i.e. a way to test it interactively? Can we expect it to stay in future versions?

    Just out of interest, do you see Rosalyn competing with reflection emit in the future? Essentially they're both ways of dynamically creating assemblies, right?

  • Mike - C# Interactive window is here to stay :)

    Roslyn will certainly compete with Reflection.Emit as a more comfortable way to generate code at runtime. However it is important to realize that with Reflection.Emit you can create code that can never be output from a C# compiler. And Roslyn can only compile valid C#, so you could say that programs compilable with Roslyn is a subset of programs compilable with Reflection.Emit.

    Kirill

  • what is the Roslyn project roadmap?

  • I tried today to install a new VM with Only Win7 and VS11 Preview + Roslyn.

    It seems that the Roslyn CTP is not design at all to be installed as an extension of VS2011.

    Is there a way to install it anyway, or should i have to install VS2010 + SP1 + Async CTP 3 to use Roslyn ?

  • It would be great if we could have DLR support. Writing Roslyn scripts in IronPython would be so much cleaner!

  • Wow Roslyn works like my old friend Clipper and its CodeBlock, but now in .NET

  • sadf

  • How do I open the C# REPL _outside_ Visual Studio? ( analogous to F# 's `fsi.exe` )

  • @Matt Hickford:

    At the moment, the C# Interactive is only supported inside Visual Studio. We do have some ideas for this, but nothing is implemented at this time.

  • looks amazing, can I use this to find all dependencies/usage of a certain class or method ?

  • I recall thinking this was built into vs2012, but checking on it now, it seems to still be stuck at ctp level. Has this been abandoned?

Page 1 of 2 (17 items) 12