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 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 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 TestClassOperator() { Delegate showResult = EHandler.CreateMethod <ENull>((il) => { EMethod methodInfoHelper = typeof(Console); EModel model = EModel.CreateModel <TestClass>().UseDefaultConstructor(); model.Set("Age", 10); EModel model2 = EModel.CreateModel <TestClass>().UseDefaultConstructor(); model2.Set("Age", 11); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") + model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") - model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") * model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") / model2.LoadValue("Age")); methodInfoHelper.ExecuteMethod <int>("WriteLine", model.LoadValue("Age") + 1000); }).Compile(); ((Action)showResult)(); }
public static void T2() { ulong? model = 1; FieldInfo info = typeof(ulong?).GetField("value", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); object obj = info.GetValue(model); Delegate test = EHandler.CreateMethod <ulong>((il) => { EModel modelHandler = EModel.CreateModelFromObject(model); //EModel modelHandler = EModel.CreateModel<ulong?>().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.LoadValue("value"); //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 <ulong> action1 = (Func <ulong>)test; ulong result1 = action1(); }