Skip to content

使用roslyn方案打造高性能动态代码,包括动态构建,运行时编译,动态调用,深度克隆,实体快照,远程调用等。(Use Roslyn to create efficient dynamic code. Including dynamic build, dynamic call, deep clone, entity snapshot, remote call and so on.)

License

fossabot/Natasha

 
 

Repository files navigation

Natasha

CI Build Status

Member project of .NET Core Community GitHub license FOSSA Status

Platform Build Server Master Status Nuget Version
Travis Linux Build Status NuGet Badge

Build history

重启项目,使用roslyn方案,去除IL操作,像正常人一样创造你的动态代码。

欢迎参与讨论:点击加入Gitter讨论组

使用 FastMethodOperator 快速构建函数:

var action = FastMethodOperator.New
             .Param<string>("str1")
             .Param(typeof(string),"str2")
             .MethodBody("return str1+str2;")
             .Return<string>()
             .Complie<Func<string,string,string>>();
                    
string result = action("Hello ","World!");    //result:   "Hello World!"

使用 DelegateOperator 快速实现委托:

//定义一个委托
public delegate string GetterDelegate(int value);
     
     
var action = DelegateOperator<GetterDelegate>.Create("value += 101; return value.ToString();");
     
string result = action(1);              //result: "102"
     

使用 FakeMethodOperator 快速构建函数:

public class Test{ 

   public string Handler(string str){ 
        retrurn null; 
   }
   
}
var action = FakeMethodOperator.New
             .UseMethod(typeof(Test).GetMethod("Handler"))
             .StaticMethodContent(" str += "" is xxx;"",return str; ")
             .Complie<Func<string,string>>();
                  
string result = action("xiao");              //result: "xiao is xxx;"          

使用Natasha的类扩展

Example:  
        Type : Dictionary<string,List<int>>[] 
        
        typeof(Dictionary<string,List<int>>).GetDevelopName();     //result:  "Dictionary<String,List<Int32>>[]"
        typeof(Dictionary<string,List<int>>).GetAvailableName();   //result:  "Dictionary_String_List_Int32____"
        typeof(Dictionary<string,List<int>>).GetAllGenericTypes(); //result:  [string,list<>,int]
        typeof(Dictionary<string,List<int>>).IsImplementFrom<IDictionary>(); //result: true

动态调用普通类

public class A{
   public int Age;
   public DateTime Time;
   public B Outter = new B();
}
public class B{
   public string Name;
   public B(){
      Name = "小明"
   }
}
//如果是运行时动态生成类,也同样

调用方式一

var handler = DynamicOperator.GetOperator(typeof(A));

handler["Age"].IntValue = 100;                                    // Set Operator

Console.WriteLine(handler["Time"].DateTime);                      // Get Operator

handler["Outter"].OperatorValue["Name"].StringValue = "NewName"   // Link Operator

调用方式二

var handler EntityOperator.Create(typeof(A));

handler.Set("Age",100);                                           // Set Operator

Console.WriteLine(handlerGet<DateTime>("Time"));                  // Get Operator

handler.Get("Outter")["Name"].Set("NewName");                     // Link Operator

动态调用静态类

public static class A{
   public static int Age;
   public static DateTime Time;
   public static B Outter = new B();
}
public class B{
   public string Name;
   public B(){
      Name = "小明"
   }
}
//如果是运行时动态生成类,也同样

调用方式一

DynamicStaticOperator handler = typeof(A);

handler["Age"].IntValue = 100;                                        // Set Operator

Console.WriteLine(handler["Time"].DateTime);                          // Get Operator

handler.Get["Outter"].OperatorValue["Name"].StringValue = "NewName"   // Link Operator

调用方式二

var handler EntityOperator.Create(type);

handler["Age"].Set(100);                                          // Set Operator

Console.WriteLine(handler["Time"].Get<DateTime>());               // Get Operator

handler.Get("Outter").Set(Name,"NewName");                        // Link Operator


  • 测试计划(等待下一版本bechmark)

    • 动态函数性能测试(对照组: emit, origin)
    • 动态调用性能测试(对照组: 动态直接调用,动态代理调用,emit, origin)
    • 动态克隆性能测试(对照组: origin)
    • 远程动态封装函数性能测试(对照组: 动态函数,emit, origin)

About

使用roslyn方案打造高性能动态代码,包括动态构建,运行时编译,动态调用,深度克隆,实体快照,远程调用等。(Use Roslyn to create efficient dynamic code. Including dynamic build, dynamic call, deep clone, entity snapshot, remote call and so on.)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 96.8%
  • R 2.5%
  • HTML 0.7%