var tempBuilder = FastMethodOperator.New;
tempBuilder.ComplierOption.UseFileComplie(); //可根據需求選擇編譯方式 var builder?=?new?ClassBuilder(); builder.ComplierOption.UseFileComplie(); builder.ComplierOption.UseMemoryComplie();
實現抽象類與接口
public abstract class TestAbstract
{ public int Name; public int Age; public abstract int GetAge(); public abstract string GetName();
} OopOperator<TestAbstract>?abstractBuilder?=?new?OopOperator<TestAbstract>();
abstractBuilder.ClassName("UTestClass");
abstractBuilder["GetName"]?=?"return?Name;";
abstractBuilder["GetAge"] = "return Age;";
abstractBuilder.Compile();
TestAbstract test = abstractBuilder.Create("UTestClass"); public interface ITest
{ int?MethodWidthReturnInt(); string?MethodWidthReturnString(); void?MethodWidthParamsRefInt(ref?int?i); string?MethodWidthParamsString(string?str); string?MethodWidthParams(int?a,string?str,int?b);
} OopOperator<ITest>?interfaceBuilder?=?new?OopOperator<ITest>();
interfaceBuilder.ClassName("UTestClass");
interfaceBuilder["MethodWidthReturnInt"]?=?"return?123456;";
interfaceBuilder["MethodWidthReturnString"]?=?"return?\"test\";";
interfaceBuilder["MethodWidthParamsRefInt"]?=?"i+=10;";
interfaceBuilder["MethodWidthParamsString"]?=?"return?str+\"1\";";
interfaceBuilder["MethodWidthParams"]?=?"return?a.ToString()+str+b.ToString();";
interfaceBuilder.Compile();
ITest?test?=?interfaceBuilder.Create("UTestClass");
快速編寫委托
public delegate string GetterDelegate(int value); //方法一
var action = DelegateOperator<GetterDelegate>.Create("value += 101; return value.ToString();");
//action(1); result: "102" //方法二
var action = "value += 101; return value.ToString();".Create<GetterDelegate>();
//action(1); result: "102"
定制方法
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!"
偽造方法
//這里只為演示,實際使用請用下面的靜態構造,拿到委托后可以直接運行。 public?class TestB
{ public?void?TestMethod(){}
} var action = FakeMethodOperator.New .UseMethod<TestB>("TestMethod") .MethodContent($@"Console.WriteLine(""Hello World!"");") .Complie<Action>(); //The class script :
//
// using System;
// public class N20d26dcba7e6451eaf4c4a6f4753e243
// {
// public void TestMethod()
//?????????{
// Console.WriteLine("Hello World!");
// }
// } var action = FakeMethodOperator.New .UseMethod<TestB>("TestMethod") .StaticMethodContent($@"Console.WriteLine(""Hello World!"");") .Complie<Action>(); //The class script :
//
//????using?System;
// public static class Neae0b5f6b8b94f4b9418ebc68813760b
// {
// public static void TestMethod()
//???????? {
// Console.WriteLine("Hello World!");
// }
// }
string text = @"using System;
using System.Collections;
using System.Linq;
using System.Text; namespace HelloWorld
{ public class TestIndex1 { public string Name; public int Age{get;set;} } public class TestIndex2 { public string Name; public int Age{get;set;} } public class TestIndex3 { public string Name; public int Age{get;set;} }
} namespace HelloWorld{ public struct TestStruct1{} public struct TestStruct2{} public class TestIndex4 { public string Name; public int Age{get;set;} }
}"; //根據腳本創建動態類
//尋找第二個命名空間中的第一個類
Type type = RuntimeComplier.GetClassType(text, 1,2);
Assert.Equal("TestIndex4", type.Name); //尋找第二個命名空間中的第二個結構體
type = RuntimeComplier.GetStructType(text, 2, 2);
Assert.Equal("TestStruct2", type.Name);
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();