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:
These templates will help you to get started on a new Visual Studio extension that uses Roslyn.
Reference Assemblies
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
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):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);
No comments:
Post a Comment