📜 ⬆️ ⬇️

Introduction to Microsoft “Roslyn” CTP

Why Roslyn?


In the past, our compilers worked as black boxes - you submit the program source code to an input, and you get an assembly at the output. All the knowledge and information that the compiler generates is thrown away and inaccessible for anyone's use.

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

Moreover, we open the C # and VB compilers with all their inside information, making code analysis available to you. We provide a public API and provide extension points in C # and VB language services.
')
This opens up new opportunities for VisualStudio extensions - writing powerful refactoring tools and language analysis utilities, as well as allowing anyone to use our parsers, semantic engines, code generators and scripts in their applications.

Downloading October 2011 CTP


CTP and supporting materials can be downloaded at: http://msdn.com/roslyn .

The main goal of early pre-build is to collect feedback on the design of the API and to introduce an interactive C # window (also known as REPL, Read-Eval-Print-Loop).

This first CTP is intended only for previewing; you should not use it for developing normal applications.

CTP is installed on Visual Studio 2010 SP1. You also need the Visual Studio 2010 SP1 SDK .

The first steps


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

To get started, the “Roslyn Project Overview” document will allow you to take a look at the API provided - how to work with the syntax and semantics in your program. Attached are several tutorials that will provide deep insight into various aspects of the Roslyn API.

CTP comes with a number of examples for Visual Studio Extensions, API compilers, refactoring tools, etc. Most of the examples are for C # and Visual Basic. You can open the source code of the examples from the Getting Started page.

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


These templates will help you start creating new extensions for Visual Studio using Roslyn.

Reference Assemblies



Roslyn assemblies are also installed in the GAC. Switching to Full Profile (instead of Client Profile) makes it possible to also link to Servies assemblies (which contain IDE support).

C # Interactive window



You can call C # Interactive window using View -> Other Windows -> C # Interactive Window. The interactive window works on new C # language services. The Roslyn architecture is flexible enough to ensure that IDE features such as IntelliSense and refactoring work in the same way in an ordinary editor and in an interactive window.

Currently, an interactive window is available only for C #. We are making efforts to create an interactive window for VB in the near future.

C # Script File Support (.csx)


CTP introduces the concept of scripted C # files. You can create a .csx file via File -> New File (or in any text editor, for example, notepad):


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

You can also copy pieces of code from a script file and send them to an interactive window (using the context menu or hot keys).

The script file editor is also made on new language services. Therefore, it is important to keep in mind that .csx scripts support only that part of the language that is already implemented in the Roslyn compiler. For more information, see the “Introduction to Scripting” section of the walkthrough.

Quick Roslyn API usage examples


This is an example 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 part of the language features was implemented in the current CTP. We are moving forward at a fast pace, but functions such as LINQ queries, attributes, events, dynamic, async are not yet implemented. For a complete list of unrealized things, you can visit the Roslyn forums .

Although not all features of the language are supported, the public API form is mostly filled out. Therefore, we advise you to write extensions and utilities that use Syntax, Symbols, and APIs.

We are very pleased to offer you an early preview of this technology and we will be happy to receive feedback, ideas and suggestions. Use the forums to ask questions and provide feedback, Microsoft Connect for recording errors and suggestions. You can also use the #RoslynCTP hashtag on Twitter.

Source: https://habr.com/ru/post/130903/


All Articles