Advertisement
BaSs_HaXoR

.NET Compiler Platform: "Roslyn"

Oct 18th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. /*SOURCES:
  2. BaSs_HaXoR
  3. http://blogs.msdn.com/b/csharpfaq/archive/2011/12/02/introduction-to-the-roslyn-scripting-api.aspx
  4. http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx
  5. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  6. .NET Compiler Platform ("Roslyn")
  7. 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!
  8. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  9. ======================================================================================================================================
  10. Smarter Refactorings
  11.  
  12.  
  13. Refactorings like Inline Rename build on the Compiler’s binding API to detect and resolve conflicts automatically.
  14.  
  15. Smart conflict resolution lets you trust that refactorings won’t break your code.
  16. ======================================================================================================================================
  17. Language Innovation
  18.  
  19. Try prototypes of potential language features, such as:
  20.  
  21. Primary constructors
  22. public class Point(int x, int y)
  23. Declaration expressions
  24. s.TryParse(out var x)
  25. Indexed members
  26. json.$x
  27. ======================================================================================================================================
  28. Rich Code Analysis APIs
  29.  
  30. 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.
  31. ======================================================================================================================================
  32.  
  33. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  34. The Basics
  35.  
  36. 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:
  37. */
  38. using Roslyn.Scripting.CSharp;
  39.  
  40. namespace RoslynScriptingDemo
  41. {
  42.     class Program
  43.     {
  44.         static void Main(string[] args)
  45.         {
  46.             var engine = new ScriptEngine();
  47.             engine.Execute(@"System.Console.WriteLine(""Hello Roslyn"");");
  48.         }
  49.     }
  50. }
  51. /*The solution needs reference.
  52.  
  53.  
  54. =====================================================================================================================================
  55. Execution Context
  56.  
  57. 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:
  58. */
  59. var engine = new ScriptEngine();
  60.  
  61. engine.Execute(@"var a = 42;");
  62. engine.Execute(@"System.Console.WriteLine(a);");
  63. /*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.
  64. */
  65. using Roslyn.Scripting.CSharp;
  66. using Roslyn.Scripting;
  67.  
  68. namespace RoslynScriptingDemo
  69. {
  70.     class Program
  71.     {
  72.         static void Main(string[] args)
  73.         {
  74.             var engine = new ScriptEngine();
  75.             var session = Session.Create();
  76.  
  77.             engine.Execute(@"var a = 42;", session);
  78.             engine.Execute(@"System.Console.WriteLine(a);", session);
  79.         }
  80.     }
  81. }
  82. //We can even define a new method, store it in the session, and then invoke it like this.
  83.  
  84. engine.Execute(@"void Foo() { System.Console.WriteLine(""Foo""); }", session);
  85. engine.Execute(@"Foo();", session);
  86. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement