Microsoft.CodeAnalysis 入門


  1 安裝 Microsoft.CodeAnalysis 

      我這里創建的是WPF的項目,首先再VS2015中用NuGet控制台進行安裝

     Install-Package Microsoft.CodeAnalysis

     Install-Package Microsoft.CodeAnalysis.CSharp.Scripting

 

    2 using 添加

1 using Microsoft.CodeAnalysis.Scripting;
2 using Microsoft.CodeAnalysis.CSharp.Scripting;
3 using Microsoft.CodeAnalysis.CSharp;
4 using Microsoft.CodeAnalysis.CSharp.Syntax;

   3 Hello World

1  #region 01 Hello world
2  //Hello Rolysn
3   Console.WriteLine(CSharpScript.RunAsync(
4                     @"string ret =""Hello Rolysn"";")
5                     .Result.GetVariable("ret").Value);
6 
7  #endregion

4 引用和命名空間

 1 #region 02 References and Namespaces
 2 ScriptOptions scriptOptions = ScriptOptions.Default;
 3 #region 引用配置
 4 //Add reference to mscorlib
 5 var mscorlib = typeof(System.Object).Assembly;
 6 var systemCore = typeof(System.Linq.Enumerable).Assembly;
 7 scriptOptions = scriptOptions.AddReferences(mscorlib);
 8 scriptOptions = scriptOptions.AddReferences(systemCore);
 9 scriptOptions = scriptOptions.AddImports("System", "System.Linq", "System.Collections.Generic");
10 #endregion
11 var state = CSharpScript.RunAsync(@"int x=2;int y=3;int z =x*y;return z+1;");
12 //int x=2 => 2
13 string x = state.Result.GetVariable("x").Value.ToString();
14 //return return z+1 => 7 
15 string retvalue = state.Result.ReturnValue.ToString();
16 //Int32
17 Type xtype = state.Result.GetVariable("x").Value.GetType();
18 if(xtype.FullName == typeof(int).FullName)
19 {
20 
21 }
22 //int z =x*y => 6
23 string z =state.Result.GetVariable("z").Value.ToString();
24 
25 string sourceCode = state.Result.Script.Code;
26 
27 string output = "";
28 foreach (var p in state.Result.Variables)
29 {
30     output += string.Format("name:{0},type:{1},value:{2};", p.Name, p.Type, p.Value);
31 }
32 Console.WriteLine(output);
33 #endregion
 1 #region 03 SyntaxTree
 2 var tree = CSharpSyntaxTree.ParseText(@"
 3         public class MyClass
 4         {
 5             public int FnSum(int x,int y)
 6             {
 7                     return x+y;      
 8             }
 9         }");
10 
11 var syntaxRoot = tree.GetRoot();
12 var MyClass = syntaxRoot.DescendantNodes().OfType<ClassDeclarationSyntax>().First();
13 var MyMethod = syntaxRoot.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
14 
15 Console.WriteLine(MyClass.Identifier.ToString());
16 Console.WriteLine(MyMethod.Identifier.ToString());
17 Console.WriteLine(MyMethod.ParameterList.ToString());
18 #endregion

5  宿主對象交互

1 #region 04 Globals
2                
3 var state4 = CSharpScript.RunAsync(@"var ret = requestData + "" from globals""; ", globals: new Globals { requestData = "hello world" });
4 Console.WriteLine( state4.Result.GetVariable("ret").Value);
5 
6 #endregion
1     public class Globals
2     {
3         /// <summary>
4         /// 可以在腳本中直接進行調用
5         /// </summary>
6         public string requestData { get; set; }
7       
8     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM