public static void TestCreateModel() { ClassBuilder builder = ClassBuilder.CreateModel("Hello"); builder.CreateDefaultConstructor(); builder.CreateField <string>("Age", FieldAttributes.Public); builder.CreateProperty <string>("Name"); builder.CreateMethod <ENull>("Show", MethodAttributes.Public, (classModel) => { classModel.SField("Age", "This is Age."); classModel.SProperty("Name", "This is name."); classModel.LPropertyValue("Name"); classModel.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); classModel.il.REmit(OpCodes.Ret); }); builder.EndBuilder(); Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EModel Model = EModel.CreateDynamicClass("Hello").UseDefaultConstructor(); EMethod.Load(Model).ExecuteMethod("Show"); Model.LFieldValue("Age"); Model.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); }).Compile(); ((Action)ShowDelegate)(); }
public static void TestClassClone() { SingleModel t = new SingleModel(); t.TestStructArray = new TestStruct[5]; t.TestStructArray[3] = new TestStruct { Age = 666 }; t.TestClassArray = new TestClass[15]; t.TestClassArray[10] = new TestClass() { Name = "xxxx" }; t.TestEnumArray = new TestEnum[16]; t.TestEnumArray[15] = TestEnum.Address; t.Name = "小明"; t.Age = 10; t.Set(); Delegate ShowDelegate = EHandler.CreateMethod <SingleModel>((il) => { EModel model = EModel.CreateModelFromObject(t); model.Load(); }).Compile(); SingleModel t2 = ((Func <SingleModel>)ShowDelegate)(); Console.WriteLine(t2.Name); Console.WriteLine(t2.Age); t2.Show(); Console.WriteLine(t2.TestStructArray[3].Age); Console.WriteLine(t2.TestClassArray[10].Name); Console.WriteLine(t2.TestEnumArray[15]); }
public static SetterDelegate SetterFunc(ClassStruction model, PropertyInfo info) { if (info.GetSetMethod(true) == null || info.GetSetMethod(true).IsPrivate) { return(null); } return((SetterDelegate)(EHandler.CreateMethod <object, object, ENull>((til) => { LocalBuilder builder = null; if (!info.GetSetMethod(true).IsStatic) { builder = til.DeclareLocal(model.TypeHandler); til.REmit(OpCodes.Ldarg_0); til.UnPacket(model.TypeHandler); til.REmit(OpCodes.Stloc_S, builder.LocalIndex); } EModel localModel = EModel.CreateModelFromBuilder(builder, model.TypeHandler); localModel.SProperty(info.Name, () => { til.REmit(OpCodes.Ldarg_1); til.UnPacket(info.PropertyType); }); }, "Setter " + info.DeclaringType.Name + " " + info.Name).Compile(typeof(SetterDelegate)))); }
public void TestProperty() { PropertyStruct model = new PropertyStruct(); model.ValueProperty = 100; model.RefProperty = "Test"; PropertyStruct.StaticRefProeprty = "Static"; PropertyStruct.StaticValueProperty = 200; Delegate test = EHandler.CreateMethod <PropertyStruct>((il) => { EModel modelHandler = EModel.CreateModelFromObject(model); modelHandler.Set("ValueProperty", modelHandler.DLoad("ValueProperty").Operator + modelHandler.DLoad("StaticValueProperty").DelayAction); modelHandler.Set("StaticValueProperty", modelHandler.DLoad("ValueProperty").Operator + modelHandler.DLoad("StaticValueProperty").DelayAction); modelHandler.Set("RefProperty", modelHandler.DLoad("RefProperty").Operator + modelHandler.DLoad("StaticRefProeprty").DelayAction); modelHandler.Load(); }).Compile(); Func <PropertyStruct> action = (Func <PropertyStruct>)test; PropertyStruct.StaticValueProperty = 200; PropertyStruct result = action(); Assert.Equal((ulong)300, result.ValueProperty); Assert.Equal((ulong)500, PropertyStruct.StaticValueProperty); Assert.Equal("TestStatic", result.RefProperty); }
public void TestProperty() { ClassWithNullableModel model = new ClassWithNullableModel(); model.ValueField = 100; model.ValueProperty = null; ClassWithNullableModel.StaticValueField = null; ClassWithNullableModel.StaticValueProperty = 200; Delegate test = EHandler.CreateMethod <ClassWithNullableModel>((il) => { EModel modelHandler = EModel.CreateModelFromObject(model); modelHandler.Set("ValueProperty", modelHandler.DLoad("PrivateProperty").DLoad("Value").Operator + modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator); modelHandler.Set("ValueProperty", modelHandler.DLoad("PrivateProperty").DLoad("Value").Operator + modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator); modelHandler.Set("StaticValueProperty", modelHandler.DLoad("ValueProperty").DLoad("Value").Operator + modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator); modelHandler.Set("ValueField", modelHandler.DLoad("StaticValueProperty").DLoad("Value").Operator + modelHandler.DLoad("ValueField").DLoad("Value").Operator); modelHandler.Load(); }).Compile(); Func <ClassWithNullableModel> action = (Func <ClassWithNullableModel>)test; ClassWithNullableModel result = action(); Assert.Equal((ulong)201, result.ValueProperty); Assert.Equal((ulong)401, ClassWithNullableModel.StaticValueProperty); Assert.Equal((ulong)501, result.ValueField); }
public void TestMethod() { Delegate test = EHandler.CreateMethod <ulong>((il) => { EModel modelHandler = EModel.CreateModel <MethodStruct>().UseDefaultConstructor(); EMethod.Load(modelHandler).ExecuteMethod("GetULongMax"); }).Compile(); Func <ulong> action = (Func <ulong>)test; Assert.Equal(ulong.MaxValue, action()); test = EHandler.CreateMethod <ulong>((il) => { EModel modelHandler = EModel.CreateModel <MethodStruct>().UseDefaultConstructor(); EMethod.Load(modelHandler).ExecuteMethod("GetULongMin"); }).Compile(); action = (Func <ulong>)test; Assert.Equal(ulong.MinValue, action()); test = EHandler.CreateMethod <string>((il) => { EModel modelHandler = EModel.CreateModel <MethodStruct>().UseDefaultConstructor(); EVar param1 = "Hello"; EMethod.Load(modelHandler).ExecuteMethod <string, string>("GetString", param1, " World"); }).Compile(); Func <string> action1 = (Func <string>)test; Assert.Equal("Hello World", action1()); }
public static void StaticClass() { Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel test = EModel.CreateModel <TestClass>().UseDefaultConstructor(); test.SField("NormalField", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("NormalField")); test.SField("StaticField", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("StaticField")); test.SField("Ref_StaticField", "10"); methodInfoHelper.ExecuteMethod <string>("WriteLine", test.LoadValue("Ref_StaticField")); test.SProperty("NormalProperty", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("NormalProperty")); test.SProperty("Ref_NormalProperty", "10"); methodInfoHelper.ExecuteMethod <string>("WriteLine", test.LoadValue("Ref_NormalProperty")); test.SProperty("StaticProperty", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", test.LoadValue("StaticProperty")); test.SProperty("Ref_StaticProperty", "10"); methodInfoHelper.ExecuteMethod <string>("WriteLine", test.LoadValue("Ref_StaticProperty")); }).Compile(); ((Action)ShowDelegate)(); }
public static void TestClassAndStruct() { //动态创建Action委托 Delegate newMethod = EHandler.CreateMethod <ENull>((il) => { EModel model = null; //测试类的字段 //model = EModel.CreateModel<ClassField>().UseDefaultConstructor(); //测试类的属性 //model = EModel.CreateModel<ClassProperty>().UseDefaultConstructor(); //测试结构体的字段 model = EModel.CreateModel <StructField>(); //测试结构体的属性 //model = EModel.CreateModel<StructProperty>(); model.Set("PublicName", "This is Public-Name"); model.Set("PrivateName", "This is Private-Name"); model.Set("PublicAge", 666); model.Set("PrivateAge", 666); EMethod method = typeof(Console); method.ExecuteMethod <string>("WriteLine", model.DLoadValue("PrivateName").DelayAction); method.ExecuteMethod <string>("WriteLine", model.LoadValue("PublicName")); method.ExecuteMethod <int>("WriteLine", model.LoadValue("PublicAge")); method.ExecuteMethod <int>("WriteLine", model.LoadValue("PrivateAge")); }).Compile(); ((Action)newMethod)(); }
public void TestClass() { ComplexClassModel model = new ComplexClassModel(); model.FieldModel = new FieldStruct(); model.PropertyModel = new PropertyStruct(); model.MethodModel = new MethodStruct(); ComplexClassModel.Model = model; Delegate test = EHandler.CreateMethod <ComplexClassModel>((il) => { EVar ulongMinHandler = ulong.MinValue; EModel modelHandler = EModel.CreateModelFromObject(model); modelHandler.Load("FieldModel").Set("RefField", "Hello"); modelHandler.Load("PropertyModel").Set("RefProperty", "Hello"); modelHandler.Load("PropertyModel").Set("ValueProperty", ulongMinHandler); modelHandler.Load("Model").Load("FieldModel").Set("RefField", "Hello1"); modelHandler.Load("Model").Load("FieldModel").Set("ValueField", () => { EMethod.Load(modelHandler.DLoad("MethodModel").Operator).ExecuteMethod("GetULongMax"); }); modelHandler.Load(); }).Compile(); Func <ComplexClassModel> action = (Func <ComplexClassModel>)test; ComplexClassModel result = action(); Assert.Equal((ulong)0, result.FieldModel.ValueField); Assert.Equal(ulong.MinValue, result.PropertyModel.ValueProperty); Assert.Equal("Hello", result.FieldModel.RefField); Assert.Equal("Hello", result.PropertyModel.RefProperty); Assert.Equal(ulong.MaxValue, ComplexClassModel.Model.FieldModel.ValueField); Assert.Equal("Hello1", ComplexClassModel.Model.FieldModel.RefField); //Assert.Equal("Hello1", model.FieldModel.RefField); }
public static void TestStructClone() { EReflector.Create(typeof(SingleModel)); TestStruct t = new TestStruct(); t.TEnum = TestEnum.Address; t.Set(); t.Name = "小明"; t.Name1 = "小明1"; t.Age = 10; t.Age1 = 101; Delegate ShowDelegate = EHandler.CreateMethod <TestStruct>((il) => { EMethod methodInfoHelper = typeof(Console); EModel model = EModel.CreateModelFromObject(t); model.SField("PrivateFAge", 10); model.LFieldValue("PrivateFAge"); methodInfoHelper.ExecuteMethod <int>("WriteLine"); model.Load(); }).Compile(); TestStruct t2 = ((Func <TestStruct>)ShowDelegate)(); Console.WriteLine(t2.Name); Console.WriteLine(t2.Age); Console.WriteLine(t2.Name1); Console.WriteLine(t2.Age1); t2.Show(); Console.WriteLine(t.TEnum); Console.WriteLine(t2.TEnum); }
public static void TestJudge() { Action action = (Action)(EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EVar Int_1 = 2; EVar Int_2 = 2; EArray objectArray = EArray.CreateArraySpecifiedLength <object>(2); objectArray.StoreArray(0, Int_1.InStackAndPacket); objectArray.StoreArray(1, Int_2.InStackAndPacket); EJudge. If(Int_1 > 2)(() => { EVar str = "{0}>{1}"; methodInfoHelper.ExecuteMethod <string, object[]>("WriteLine", str, objectArray); }) .ElseIf(Int_1 == Int_2)(() => { EVar str = "{0}={1}"; methodInfoHelper.ExecuteMethod <string, object[]>("WriteLine", str, objectArray); }) .Else(() => { EVar str = "{0}<{1}"; methodInfoHelper.ExecuteMethod <string, object[]>("WriteLine", str, objectArray); }); }).Compile()); action(); }
public static void T <T>() { ClassWithNullableModel model = new ClassWithNullableModel(); model.ValueField = 100; model.ValueProperty = null; ClassWithNullableModel.StaticValueField = 100; ClassWithNullableModel.StaticValueProperty = 200; Delegate test = EHandler.CreateMethod <ClassWithNullableModel>((il) => { EModel modelHandler = EModel.CreateModelFromObject(model); //EModel modelHandler = EModel.CreateModel<ClassWithNullableModel>().UseDefaultConstructor(); //modelHandler.Set("<ValueProperty>k__BackingField", (ulong)100); //modelHandler.Set("ValueField", (ulong)100); //modelHandler.Set("StaticValueField", (ulong)100); //modelHandler.Set("ValueProperty", (ulong)100); //modelHandler.Set("<StaticValueProperty>k__BackingField", (ulong)300); //modelHandler.LoadValue("<ValueProperty>k__BackingField").Packet(); //FieldInfo info = typeof(ClassWithNullableModel).GetField("<PrivateProperty>k__BackingField", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); //il.REmit(OpCodes.Ldtoken, typeof(ClassWithNullableModel)); //il.REmit(OpCodes.Call, ClassCache.ClassHandle); //il.REmit(OpCodes.Ldstr, "<PrivateProperty>k__BackingField"); //il.REmit(OpCodes.Ldc_I4_S, 60); //il.REmit(OpCodes.Callvirt, ClassCache.FieldInfoGetter); //LocalBuilder field = il.DeclareLocal(typeof(FieldInfo)); //il.REmit(OpCodes.Stloc_S, field.LocalIndex); //il.LoadBuilder(field); ////il.REmit(OpCodes.Callvirt, ClassCache.FieldInfoGetter); //modelHandler.InStackAndPacket(); //il.REmit(OpCodes.Callvirt, ClassCache.FieldValueGetter); //, modelHandler.DLoad("PrivateProperty").DLoad("Value").DelayAction //modelHandler.Load("<PrivateProperty>k__BackingField").LoadValue("value"); //EMethod.Load(typeof(Console)).ExecuteMethod<ulong>("WriteLine"); //modelHandler.Set("ValueProperty", modelHandler.DLoad("PrivateProperty").DLoad("Value").DelayAction); //EMethod.Load(typeof(Console)).ExecuteMethod<ulong>("WriteLine", modelHandler.DLoad("ValueProperty").DLoad("Value").DelayAction); //EMethod.Load(typeof(Console)).ExecuteMethod<ulong>("WriteLine", modelHandler.DLoad("PrivateProperty").DLoad("Value").DelayAction); //modelHandler.LoadValue("ValueField").Packet(); //EMethod.Load(typeof(Console)).ExecuteMethod<object>("WriteLine"); //modelHandler.LoadValue("ValueProperty").Packet(); //EMethod.Load(typeof(Console)).ExecuteMethod<object>("WriteLine"); //modelHandler.LoadValue("StaticValueField").Packet(); //EMethod.Load(typeof(Console)).ExecuteMethod<object>("WriteLine"); //modelHandler.LoadValue("StaticValueProperty").Packet(); //EMethod.Load(typeof(Console)).ExecuteMethod<object>("WriteLine"); //EMethod.Load(typeof(Console)).ExecuteMethod<ulong>("WriteLine", modelHandler.DLoad("ValueField").DelayAction); //EMethod.Load(typeof(Console)).ExecuteMethod<object>("WriteLine", modelHandler.DLoad("StaticValueProperty").DelayAction); //modelHandler.Set("ValueProperty", modelHandler.DLoad("ValueProperty").Operator + modelHandler.DLoad("StaticValueProperty").DelayAction); //modelHandler.Set("StaticValueProperty", modelHandler.DLoad("ValueProperty").Operator + modelHandler.DLoad("StaticValueProperty").DelayAction); // modelHandler.Set("RefProperty", modelHandler.DLoad("RefProperty").Operator + modelHandler.DLoad("StaticRefProeprty").DelayAction); modelHandler.Load(); }).Compile(); Func <ClassWithNullableModel> action1 = (Func <ClassWithNullableModel>)test; ClassWithNullableModel t = action1(); }
public void StringOperator() { Delegate test = EHandler.CreateMethod <string>((il) => { EVar temp_var1 = "Hello "; EVar temp_var2 = EVar.CreateVarFromObject("World"); (temp_var1 + temp_var2)(); }).Compile(); Func <string> action = (Func <string>)test; Assert.Equal("Hello World", action()); }
public void TestString() { string[] testArray = new string[] { "0", "1", "2", "3", "4", "5" }; for (int i = 0; i < testArray.Length; i += 1) { Delegate ShowDelegate = EHandler.CreateMethod <string>((il) => { EArray Model = testArray; Model.LoadArray(i); }).Compile(); Assert.Equal(i.ToString(), ((Func <string>)ShowDelegate)()); } }
/// <summary> /// 使用Natasha根据参数数组信息生成Command高速构建缓存 /// </summary> /// <typeparam name="T">实际执行函数返回的类型</typeparam> /// <param name="sql">SQL语句</param> /// <param name="values">object参数数组</param> /// <returns>动态方法</returns> private static SqlDelegate <T> .GetCommandByObject GetEmitCommandCache <T>(string sql, object[] values) { Type returnType = typeof(T); if (!Cache.SqlCache.ContainsKey(returnType)) { ModelAnalyser.Initialization(returnType); } Delegate newMethod = EHandler.CreateMethod <ERef <IDbCommand>, object[], ENull>((il) => { EModel idbCommand = EModel.CreateModelFromParameter <IDbCommand>(0); idbCommand.UseRef(); idbCommand.Set("CommandText", sql); EArray arrayArg = EArray.CreateArrayFromParameter(1, typeof(object)); EModel copyParameters = idbCommand.Load("Parameters"); MatchCollection collection = ParameterRegex.Matches(sql); int length = collection.Count; for (int i = 0; i < length; i += 1) { Type type = values[i].GetType(); string memberName = collection[i].Groups[1].Value; copyParameters.Dup(); EModel copyParameter = EMethod.Load(idbCommand).ExecuteMethod("CreateParameter").Dup(); copyParameter.Set("ParameterName", "@".Append(memberName)); copyParameter.Dup().Set("DbType", (int)SqlTypes[type]); if (type.IsPrimitive) { copyParameter.Dup().Set("Value", () => { arrayArg.LoadArray(i); }); } else { EJudge.If(ENull.IsNull(() => { arrayArg.LoadArray(i); }))(() => { copyParameter.Dup().Set("Value", EDBNull.LoadValue); }).Else(() => { copyParameter.Dup().Set("Value", () => { arrayArg.LoadArray(i); }); }); } EMethod.Load <IList>().ExecuteMethod <object>("Add").Pop(); } copyParameters.Pop(); }).Compile(typeof(SqlDelegate <T> .GetCommandByObject)); return((SqlDelegate <T> .GetCommandByObject)newMethod); }
public static void ForeachClassArray() { TestClass[] testArray = new TestClass[5]; for (int i = 0; i < testArray.Length; i += 1) { TestClass t = new TestClass(); t.Name = "T" + i; testArray[i] = t; } testArray[0].FieldNext = new TestStruct() { Name = "1", Age = 10 }; Delegate ShowDelegate = EHandler.CreateMethod <TestClass[]>((il) => { EArray model = testArray; ELoop.For(model, (loadCurrentElement) => { EModel modelHandler = EModel.CreateModelFromAction <TestClass>(loadCurrentElement); modelHandler.LField("FieldNext").LFieldValue("Age"); modelHandler.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) })); }); model.Load(); }).Compile(); if (ShowDelegate != null) { TestClass[] result = ((Func <TestClass[]>)ShowDelegate)(); for (int i = 0; i < result.Length; i += 1) { result[i].Name = "T" + (i + 5); } Console.WriteLine(result[0].FieldNext.Age); Console.WriteLine("旧对象:"); for (int i = 0; i < testArray.Length; i += 1) { Console.WriteLine(testArray[i].Name); } Console.WriteLine("深度复制新对象:"); for (int i = 0; i < result.Length; i += 1) { Console.WriteLine(result[i].Name); } } else { Console.WriteLine("??"); } }
public static void ForeachIntArray() { int[] testArray = new int[] { 1, 2, 3, 4, 5 }; Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EArray Model = testArray; ELoop.For(Model, (loadCurrentElement) => { loadCurrentElement(); Model.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(int) })); }); }).Compile(); ((Action)ShowDelegate)(); }
public static void ForeachStringArray() { string[] testArray = new string[] { "1", "2", "C", "D" }; Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EArray Model = testArray; ELoop.For(Model, (loadCurrentElement) => { loadCurrentElement(); Model.il.REmit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) })); }); }).Compile(); ((Action)ShowDelegate)(); }
public void TestInt() { int[] testArray = new int[] { 1, 2, 3, 4, 5 }; Delegate ShowDelegate = EHandler.CreateMethod <int>((il) => { EVar result = EVar.CreateVar <int>(); EArray Model = testArray; ELoop.For(Model, (loadCurrentElement) => { result.Store(result + loadCurrentElement); }); result.Load(); }).Compile(); Assert.Equal(15, ((Func <int>)ShowDelegate)()); }
public static void TestSingle() { //动态创建Action委托 Delegate newMethod = EHandler.CreateMethod <ENull>((il) => { //创建没有临时变量的普通入栈变量(没有临时变量,所以自加操作没有意义) EVar intHandler = 1; //创建函数操作句柄 EMethod method = typeof(Console); //输出intHandler的时候,让变量做加法运算。 method.ExecuteMethod <int>("WriteLine", intHandler + 665); //结果:666; }).Compile(); ((Action)newMethod)(); //动态创建Action<int,string>委托 Delegate newMethod1 = EHandler.CreateMethod <ENull>((il) => { //创建有临时变量的普通入栈变量(自加操作可以被自身储存) 也就是说可以使用store存储函数 //int i = 664; EVar intHandler = EVar.CreateVarFromObject(664); //i++; intHandler++; //i=i+1; intHandler.Store(intHandler + 1); //创建函数操作句柄 EMethod method = typeof(Console); //输出intHandler method.ExecuteMethod <int>("WriteLine", intHandler); //结果:666 }).Compile(); ((Action)newMethod1)(); ////动态创建Action委托 //Delegate newMethod0 = EHandler.CreateMethod<ENull>((il) => { }).Compile(); ////动态创建Action<string,int>委托 //Delegate newMethod1 = EHandler.CreateMethod<string,int,ENull>((il) => { }).Compile(); ////动态创建Func<string>委托 //Delegate newMethod2 = EHandler.CreateMethod<string>((il) => { }).Compile(); ////动态创建Func<string,TestClass>委托 //Delegate newMethod3 = EHandler.CreateMethod<string, TestClass>((il) => { }).Compile(); }
public static void TestClass() { Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel model = EModel.CreateModel <TestClass>().UseDefaultConstructor(); model.Set("Name", "Name"); model.LoadValue("Name"); methodInfoHelper.ExecuteMethod <string>("WriteLine"); model.Set("Age", 10); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.Load("FieldNext").LoadValue("Age")); }).Compile(); ((Action)ShowDelegate)(); }
public static GetterDelegate GetterFunc(ClassStruction model, FieldInfo info) { return((GetterDelegate)(EHandler.CreateMethod <object, object>((til) => { LocalBuilder builder = null; if (!info.IsStatic) { builder = til.DeclareLocal(model.TypeHandler); til.REmit(OpCodes.Ldarg_0); til.UnPacket(model.TypeHandler); til.REmit(OpCodes.Stloc_S, builder.LocalIndex); } EModel localModel = EModel.CreateModelFromBuilder(builder, model.TypeHandler); localModel.LFieldValue(info.Name).Packet(); }, "Getter " + info.DeclaringType.Name + " " + info.Name).Compile(typeof(GetterDelegate)))); }
public void TestProperty() { Delegate test = EHandler.CreateMethod <PropertyStruct>((il) => { EModel modelHandler = EModel.CreateModel <PropertyStruct>().UseDefaultConstructor(); modelHandler.Set("ValueProperty", ulong.MinValue); modelHandler.Set("StaticValueProperty", ulong.MaxValue); modelHandler.Set("RefProperty", "Hello World"); modelHandler.Load(); }).Compile(); Func <PropertyStruct> action = (Func <PropertyStruct>)test; PropertyStruct result = action(); Assert.Equal(ulong.MinValue, result.ValueProperty); Assert.Equal(ulong.MaxValue, PropertyStruct.StaticValueProperty); Assert.Equal("Hello World", result.RefProperty); }
public static void TestIf() { Delegate showResult = EHandler.CreateMethod <ENull>((il) => { EMethod method = typeof(Console); EVar emit_A = EVar.CreateWithoutTempVar(10); EVar emit_B = EVar.CreateVarFromObject(20); TestClass t = new TestClass() { Field = 10 }; t.PropertyName = "3"; EModel model = EModel.CreateModelFromObject(t); EJudge.If(emit_A == model.DLoadValue("Field").Operator)(() => { method.ExecuteMethod <string>("WriteLine", "相等"); }).ElseIf(emit_A > emit_B)(() => { method.ExecuteMethod <int>("WriteLine", emit_A); }).Else(() => { method.ExecuteMethod <int>("WriteLine", emit_B); }); EVar string_A = "6"; EVar string_B = "2"; EJudge.If(string_A == "1")(() => { method.ExecuteMethod <string>("WriteLine", string_A); }).ElseIf(string_A == model.DLoadValue("PropertyName").Operator)(() => { method.ExecuteMethod <string>("WriteLine", string_A); }).Else(() => { method.ExecuteMethod <string>("WriteLine", string_B); }); }).Compile(); ((Action)showResult)(); }
public static void TestDictionaryClone() { Dictionary <int, string> testList = new Dictionary <int, string>(); testList[0] = "One"; testList[1] = "Two"; testList[2] = "Three"; Delegate ShowDelegate = EHandler.CreateMethod <Dictionary <int, string> >((il) => { EModel model = EModel.CreateModelFromObject(testList); model.Load(); }).Compile(); Dictionary <int, string> t2 = ((Func <Dictionary <int, string> >)ShowDelegate)(); testList.Remove(0); Console.WriteLine(testList[0]); Console.WriteLine(t2[0]); }
public static void TestNull() { //动态创建Action委托 Delegate newMethod = EHandler.CreateMethod <ENull>((il) => { //创建没有临时变量的普通入栈变量(没有临时变量,所以自加操作没有意义) EVar intHandler = EVar.CreateVarFromObject("2"); //创建函数操作句柄 EMethod method = typeof(Console); //输出intHandler method.ExecuteMethod <string>("WriteLine", intHandler); //填空值string=null intHandler.Store(ENull.Value); method.ExecuteMethod <string>("WriteLine", intHandler); }).Compile(); ((Action)newMethod)(); }
public static void TestListClone() { List <string> testList = new List <string>(); testList.Add("One"); testList.Add("Two"); testList.Add("Three"); Delegate ShowDelegate = EHandler.CreateMethod <List <string> >((il) => { EModel model = EModel.CreateModelFromObject(testList); model.Load(); }).Compile(); List <string> t2 = ((Func <List <string> >)ShowDelegate)(); testList.RemoveAt(0); Console.WriteLine(testList[0]); Console.WriteLine(t2[0]); }
public void SOTest() { ComplexStructModel model = new ComplexStructModel(); model.FieldModel = new FieldClass() { RefField = "Hello", ValueField = ulong.MaxValue - 1 }; model.PropertyModel = new PropertyClass() { RefProperty = "Hello", ValueProperty = ulong.MinValue + 1 }; model.MethodModel = new MethodClass(); ComplexStructModel.Model = model; Delegate test = EHandler.CreateMethod <ComplexStructModel>((il) => { EVar ulongHandler = (ulong)1; EVar ulongHandlerFromObject = EVar.CreateVarFromObject((ulong)1); EVar stringHandler = " World"; EModel modelHandler = EModel.CreateModelFromObject(model); modelHandler.Load("FieldModel").Set("ValueField", modelHandler.DLoad("FieldModel").DLoad("ValueField").Operator + (ulong)1); modelHandler.DLoad("FieldModel").DLoad("ValueField").Operator--; modelHandler.DLoad("FieldModel").DLoad("ValueField").Operator++; modelHandler.Load("PropertyModel").Set("ValueProperty", modelHandler.DLoad("PropertyModel").DLoad("ValueProperty").Operator - (ulong)1); modelHandler.DLoad("PropertyModel").DLoad("ValueProperty").Operator++; modelHandler.DLoad("PropertyModel").DLoad("ValueProperty").Operator--; modelHandler.Load("PropertyModel").Set("RefProperty", modelHandler.DLoad("PropertyModel").DLoad("RefProperty").Operator + " World"); modelHandler.Load("FieldModel").Set("RefField", modelHandler.DLoad("FieldModel").DLoad("RefField").Operator + stringHandler); modelHandler.DLoad("Model").DLoad("FieldModel").DLoad("ValueField").Operator++; modelHandler.Load(); }).Compile(); Func <ComplexStructModel> action = (Func <ComplexStructModel>)test; ComplexStructModel result = action(); Assert.Equal(ulong.MaxValue, result.FieldModel.ValueField); Assert.Equal(ulong.MinValue, result.PropertyModel.ValueProperty); Assert.Equal("Hello World", result.FieldModel.RefField); Assert.Equal("Hello World", result.PropertyModel.RefProperty); Assert.Equal(ulong.MaxValue, ComplexStructModel.Model.FieldModel.ValueField); }
public static void Main() { FieldClass model = new FieldClass(); model.ValueField = 100; model.RefField = "Test"; FieldClass.StaticRefField = "Static"; FieldClass.StaticValueField = 200; Delegate test = EHandler.CreateMethod <FieldClass>((il) => { EModel modelHandler = EModel.CreateModelFromObject(model); modelHandler.Set("ValueField", modelHandler.DLoad("ValueField").Operator + modelHandler.DLoad("StaticValueField").DelayAction); //modelHandler.Set("StaticValueField", modelHandler.DLoadValue("ValueField").Operator + modelHandler.DLoadValue("StaticValueField").DelayAction); //modelHandler.Set("RefField", modelHandler.DLoadValue("RefField").Operator + modelHandler.DLoadValue("StaticRefField").DelayAction); modelHandler.Load(); }).Compile(); Func <FieldClass> action = (Func <FieldClass>)test; FieldClass result = action(); }
public void DoubleOperator() { Delegate test = EHandler.CreateMethod <double>((il) => { EVar temp_var1 = (double)15; EVar temp_var2 = EVar.CreateVarFromObject((double)10); EVar temp_var3 = 3.5; temp_var2.Store(temp_var1 + temp_var2); temp_var2.Store(temp_var2 / (double)5); temp_var2.Store(temp_var2 - temp_var3); temp_var2.Store(temp_var2 * 3.0); temp_var2--; temp_var2++; temp_var2.Load(); }).Compile(); Func <double> action = (Func <double>)test; Assert.Equal(4.5, action()); }