Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*SOURCES:
- BaSs_HaXoR
- http://blogs.msdn.com/b/csharpfaq/archive/2011/12/02/introduction-to-the-roslyn-scripting-api.aspx
- http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- .NET Compiler Platform ("Roslyn")
- The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs. You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio!
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- ======================================================================================================================================
- Smarter Refactorings
- Refactorings like Inline Rename build on the Compiler’s binding API to detect and resolve conflicts automatically.
- Smart conflict resolution lets you trust that refactorings won’t break your code.
- ======================================================================================================================================
- Language Innovation
- Try prototypes of potential language features, such as:
- Primary constructors
- public class Point(int x, int y)
- Declaration expressions
- s.TryParse(out var x)
- Indexed members
- json.$x
- ======================================================================================================================================
- Rich Code Analysis APIs
- Build your own code analysis tools on top of the syntax, symbol, and binding APIs that Visual Studio uses, along with Workspaces APIs such as Find All References.
- ======================================================================================================================================
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- The Basics
- Let’s start by looking at a very simple example. Let’s just execute some code dynamically. To do that all we need is an instance of the ScriptEngine class. Specifically we need the C# version defined in Roslyn.Scripting.CSharp. All we have to do is create an instance of ScriptEngine and call the Execute method with the code to be evaluated. The Roslyn scripting version of Hello World is as simple as:
- */
- using Roslyn.Scripting.CSharp;
- namespace RoslynScriptingDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var engine = new ScriptEngine();
- engine.Execute(@"System.Console.WriteLine(""Hello Roslyn"");");
- }
- }
- }
- /*The solution needs reference.
- =====================================================================================================================================
- Execution Context
- Unfortunately the code above does have a few limitations. The first of which is that there’s no cumulative execution context, so if we define a variable as part of one code snippet and then try to access the variable from another code snippet, we will get an exception. For example, if we change the body of Main to something like this:
- */
- var engine = new ScriptEngine();
- engine.Execute(@"var a = 42;");
- engine.Execute(@"System.Console.WriteLine(a);");
- /*We will get a compilation error from the scripting engine as the statements are treated as separate compilations. Fortunately, the Scripting API provides the Session type that can be used as an execution context when calling Execute. A session stores the context of a series of submissions, so by providing a session we can execute the code above and get the expected result.
- */
- using Roslyn.Scripting.CSharp;
- using Roslyn.Scripting;
- namespace RoslynScriptingDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var engine = new ScriptEngine();
- var session = Session.Create();
- engine.Execute(@"var a = 42;", session);
- engine.Execute(@"System.Console.WriteLine(a);", session);
- }
- }
- }
- //We can even define a new method, store it in the session, and then invoke it like this.
- engine.Execute(@"void Foo() { System.Console.WriteLine(""Foo""); }", session);
- engine.Execute(@"Foo();", session);
- //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement