Giter Site home page Giter Site logo

csuffyy / natasha Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dotnetcore/natasha

0.0 2.0 0.0 9.21 MB

简化IL操作,优化IL编程流程,编写提供高性能的动态缓存,像写普通代码一样去写IL代码。

License: MIT License

C# 96.91% R 2.42% HTML 0.68%

natasha's Introduction

Natasha

Dynamic compilation of runtime code using roslyn, high performance, traceable. OnlineChat

使用roslyn动态编译运行时代码,高性能、可追踪。 欢迎参与讨论:点击加入Gitter讨论组

Member project of .NET Core Community NuGet Badge GitHub license Gitter GitHub commit activity


类库信息(Library Info)

Scan Name Status
Version GitHub tag (latest SemVer)
Lang Complie
Size GitHub repo size
Rumtime standard
OS Windows linux mac

持续构建(CI Build Status)

CI Platform Build Server Master Build Master Test
Travis Linux/OSX Build status
AppVeyor Windows/Linux Build status Build status
Azure Windows Build Status Build Status
Azure Linux Build Status Build Status
Azure Mac Build Status Build Status

发布计划(Publish Plan)

  • 2019-06-25 : 发布v0.7.1.2, 修复跨平台调用,将object类型纳入一次性赋值类型,增加类扩展方法。
  • 2019-06-26 : 发布v0.7.2.0, 升级到Standard的程序集操作,并指定release模式进行编译。
  • 2019-08-01 : 发布v1.0.0.0, 稳如老狗的版本,抛弃Emit农耕铲,端起Roslyn金饭碗。


使用方法(User Api):

首先编辑您的工程文件:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>   <--- 定要加上这句话
  </PropertyGroup>


使用 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"


//方法二
var action = "value += 101; return value.ToString();".Create<GetterDelegate>();
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;"          


动态调用普通类:

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.New();

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

Console.WriteLine(handler.Get<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 = StaticEntityOperator.Create(type);

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

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

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


方便的扩展

使用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
        typeof(Dictionary<string,List<int>>).IsOnceType();         //result: false
        typeof(List<>).With(typeof(int));                          //result: List<int>


使用Natasha的方法扩展:

Example:  

        Using : Natasha.Method; 
        public delegate int AddOne(int value);
        
        
        var action = "return value + 1;".Create<AddOne>();
        var result = action(9);
        //result : 10
        
        
        var action = typeof(AddOne).Create("return value + 1;");
        var result = action(9);
        //result : 10


使用Natasha的克隆扩展:

Example:  

        Using : Natasha.Clone; 
        var instance = new ClassA();
        var result = instance.Clone();


使用Natasha的快照扩展:

Example:  

        Using : Natasha.Snapshot; 
        var instance = new ClassA();
        
        instance.MakeSnapshot();
        
        // ********
        //  do sth
        // ********
        
        var result = instance.Compare();


使用Natasha的动态扩展:

Example:  

        Using : Natasha.Caller; 
        var instance = new ClassA();
        
        //Get DynamicHandler on the instance.
        var handler = instance.Caller();
        
        //Get Operation
        handler.Get<string>("MemberName");
        handler["MemberName"].Get<string>();
        
        //Set Operation
        handler.Set("MemberName",AnythingValue);
        handler["MemberName"].Set(AnythingValue);



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

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

License

FOSSA Status

natasha's People

Contributors

nmsazulx avatar fs7744 avatar csuffyy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.