Beispiel #1
0
        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();
        }
Beispiel #2
0
        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)();
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        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)();
        }
Beispiel #5
0
        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)();
        }
Beispiel #6
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)();
        }
Beispiel #7
0
        public static void TestOperator()
        {
            Delegate showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12);
                EVar emit_B    = EVar.CreateVarFromObject(13);
                method.ExecuteMethod <int>("WriteLine", emit_A + emit_B);
                method.ExecuteMethod <int>("WriteLine", emit_A + 1);
                method.ExecuteMethod <int>("WriteLine", 1 + 1);
                method.ExecuteMethod <int>("WriteLine", emit_B++);
            }).Compile();

            ((Action)showResult)();

            TestClass t = new TestClass();

            t.Field    = 10;
            t.Property = 20;

            TestStruct t2 = new TestStruct();

            t2.Field    = 90;
            t2.Property = 80;
            t.Next      = t2;
            showResult  = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12);
                EVar emit_B    = EVar.CreateVarFromObject(13);
                EModel model   = EModel.CreateModelFromObject(t);

                method.ExecuteMethod <int>("WriteLine", model.DLoadValue("Field").DelayAction);
                method.ExecuteMethod <int>("WriteLine", (model.DLoadValue("Field").Operator++).DelayAction);
                method.ExecuteMethod <int>("WriteLine", model.DLoadValue("Field") + emit_A);
                method.ExecuteMethod <int>("WriteLine", model.DLoadValue("Field").Operator + 10);
                method.ExecuteMethod <int>("WriteLine", (model.DLoad("Next").DLoadValue("Property").Operator++).DelayAction);
                method.ExecuteMethod <int>("WriteLine", (model.DLoad("Next").DLoadValue("Property").Operator + 10));
                method.ExecuteMethod <int>("WriteLine", emit_B + model.DLoadValue("Property").Operator);
                method.ExecuteMethod <int>("WriteLine", emit_B++);
            }).Compile();
            ((Action)showResult)();
        }
Beispiel #8
0
        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)();
        }
Beispiel #9
0
        public static void TestWhile()
        {
            Delegate showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);

                EVar emit_A = EVar.CreateWithoutTempVar(16);
                EVar emit_B = EVar.CreateVarFromObject(20);
                ELoop.While(emit_A < emit_B)(() =>
                {
                    method.ExecuteMethod <int>("WriteLine", emit_B);
                    emit_B--;
                });


                TestClass t = new TestClass()
                {
                    Field = 10
                };
                EModel model = EModel.CreateModelFromObject(t);
                ELoop.While(model.DLoadValue("Field").Operator < emit_B)(() =>
                {
                    //这里需要传递委托,不传递委托则返回的是model类型而不是int类型
                    method.ExecuteMethod <int>("WriteLine", model.DLoadValue("Field").DelayAction);
                    model.DLoadValue("Field").Operator++;
                });

                ELoop.While(model.DLoadValue("Field").Operator != 25)(() =>
                {
                    method.ExecuteMethod <int>("WriteLine", model.DLoadValue("Field").DelayAction);
                    model.DLoadValue("Field").Operator++;
                });
            }).Compile();

            ((Action)showResult)();
        }
Beispiel #10
0
        public static void TestNesting()
        {
            Delegate ShowDelegate = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod methodInfoHelper = typeof(Console);

                EModel structModel = EModel.CreateModel <TestStruct>().UseDefaultConstructor();
                structModel.SProperty("Name", "Name1");
                structModel.SField("Age", 101);


                EModel nesting_classModel = EModel.CreateModel <TestClass>().UseDefaultConstructor();
                nesting_classModel.SProperty("Name", "Name2");
                nesting_classModel.SField("Age", 102);

                EModel classModel = EModel.CreateModel <TestClass>().UseDefaultConstructor();
                classModel.SProperty("Name", "Name");
                classModel.SField("Age", 10);
                classModel.SField("FieldNext", structModel);
                classModel.SProperty("PropertyNext", nesting_classModel);

                classModel.LField("FieldNext").LFieldValue("Age");
                methodInfoHelper.ExecuteMethod <int>("WriteLine");

                classModel.LField("FieldNext").LPropertyValue("Name");
                methodInfoHelper.ExecuteMethod <string>("WriteLine");

                classModel.LPropertyValue("PropertyNext").LFieldValue("Age");
                methodInfoHelper.ExecuteMethod <int>("WriteLine");

                classModel.LPropertyValue("PropertyNext").LPropertyValue("Name");
                methodInfoHelper.ExecuteMethod <string>("WriteLine");
            }).Compile();

            ((Action)ShowDelegate)();
        }
Beispiel #11
0
        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();
        }
Beispiel #12
0
        public static void TestDefault()
        {
            Action action = (Action)(EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod methodInfoHelper = typeof(Console);
                EVar stringHandler = "16";
                EVar intHandler = 10;
                EVar doubleHandler = 0.00;
                EJudge.If(EDefault.IsDefault(doubleHandler.TypeHandler, () => { doubleHandler.Load(); }))(() =>
                {
                    EMethod.Load(typeof(Console)).ExecuteMethod <string>("WriteLine", "doubleHandler是默认值");
                }).Else(() =>
                {
                    doubleHandler.This();
                    methodInfoHelper.ExecuteMethod <double>("WriteLine");
                });
            }).Compile());

            action();
        }
Beispiel #13
0
        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)();
        }
Beispiel #14
0
        public static void TestComplexClone()
        {
            TestClass testModel = new TestClass();

            testModel.Name = "X";
            testModel.Age  = 10;

            TestStruct nestingStruct = new TestStruct();

            nestingStruct.Age  = 101;
            nestingStruct.Name = "CX";

            TestInterfaceModel interfaceModel = new TestInterfaceModel();

            interfaceModel.Shut = true;

            testModel.PInterface    = interfaceModel;
            testModel.FieldNext     = nestingStruct;
            testModel.PropertyNext1 = nestingStruct;

            Delegate ShowDelegate = EHandler.CreateMethod <TestClass>((il) =>
            {
                EMethod method    = typeof(Console);
                EModel classModel = EModel.CreateModelFromObject(testModel);
                classModel.ALoad("PrivatePName").GetAttributeModel("Attribute1").LoadValue("Name");
                method.ExecuteMethod <string>("WriteLine");
                classModel.Load();
            }).Compile();
            TestClass Result = ((Func <TestClass>)ShowDelegate)();

            Console.WriteLine(Result.Name);
            Console.WriteLine(Result.FieldNext.Name);
            Console.WriteLine(Result.FieldNext.Age);
            Console.WriteLine(Result.PropertyNext1.Name);
            Console.WriteLine(Result.PropertyNext1.Age);
            Console.WriteLine(Result.PInterface.Shut);
        }
Beispiel #15
0
        public static void DTestArray()
        {
            string[]       strArray    = new string[5];
            int[]          intArray    = new int[5];
            StructField[]  structArray = new StructField[5];
            ClassField[]   classArray  = new ClassField[5];
            DocumentEnum[] enumArray   = new DocumentEnum[5];
            for (int i = 0; i < strArray.Length; i += 1)
            {
                strArray[i]    = i.ToString();
                intArray[i]    = i;
                enumArray[i]   = DocumentEnum.ID;
                structArray[i] = new StructField()
                {
                    PublicAge = i
                };
                classArray[i] = new ClassField()
                {
                    PublicAge = i
                };
            }
            //动态创建Action委托
            Delegate newMethod = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                //从运行时获取数组并入栈到IL层临时变量
                EArray stringArrayModel = strArray;
                ELoop.For(stringArrayModel, (currentElement) =>
                {
                    method.ExecuteMethod <string>("WriteLine", currentElement);
                });

                EArray intArrayModel = intArray;
                ELoop.For(3, 5, 1, intArrayModel, (currentElement) =>
                {
                    method.ExecuteMethod <int>("WriteLine", currentElement);
                });

                EArray arrayModel = EArray.CreateArraySpecifiedLength <int>(10);
                arrayModel.StoreArray(5, 6);
                arrayModel.StoreArray(6, 6);
                arrayModel.StoreArray(7, 6);
                ELoop.For(0, 10, 1, arrayModel, (currentElement) =>
                {
                    method.ExecuteMethod <int>("WriteLine", currentElement);
                });
                //从运行时获取数组并入栈到IL层临时变量
                EArray structArrayModel = EArray.CreateArrayFromRuntimeArray(structArray);
                ELoop.For(structArrayModel, (currentElement) =>
                {
                    EModel model = EModel.CreateModelFromAction(currentElement, typeof(StructField));
                    model.LFieldValue("PublicAge");
                    method.ExecuteMethod <int>("WriteLine");
                });
                EArray classArrayModel = EArray.CreateArrayFromRuntimeArray(classArray);
                ELoop.For(classArrayModel, (currentElement) =>
                {
                    EModel model = EModel.CreateModelFromAction(currentElement, typeof(ClassField));
                    model.LFieldValue("PublicAge");
                    method.ExecuteMethod <int>("WriteLine");
                });
                EArray enumArrayModel = EArray.CreateArrayFromRuntimeArray(enumArray);
                ELoop.For(enumArrayModel, (currentElement) =>
                {
                    EModel model = EModel.CreateModelFromAction(currentElement, typeof(DocumentEnum));
                    model.Load();
                    EPacket.Packet(typeof(DocumentEnum));
                    method.ExecuteMethod <object>("WriteLine");
                });
            }).Compile();

            ((Action)newMethod)();
        }
Beispiel #16
0
        public static void TestOperaotr()
        {
            Delegate showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12);
                EVar emit_B    = EVar.CreateWithoutTempVar(13);
                method.ExecuteMethod <int>("WriteLine", emit_A + emit_B);
            }).Compile();

            ((Action)showResult)();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12);
                EVar emit_B    = EVar.CreateWithoutTempVar(13);
                method.ExecuteMethod <int>("WriteLine", emit_A - emit_B);
            }).Compile();

            ((Action)showResult)();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12);
                EVar emit_B    = EVar.CreateWithoutTempVar(4);
                method.ExecuteMethod <int>("WriteLine", emit_A * emit_B);
            }).Compile();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12.04);
                EVar emit_B    = EVar.CreateWithoutTempVar(4.00);
                method.ExecuteMethod <double>("WriteLine", emit_A / emit_B);
            }).Compile();

            ((Action)showResult)();


            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(12);
                EVar emit_B    = EVar.CreateWithoutTempVar(5);
                method.ExecuteMethod <int>("WriteLine", emit_A % emit_B);
            }).Compile();

            ((Action)showResult)();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(8);
                EVar emit_B    = EVar.CreateWithoutTempVar(4);
                method.ExecuteMethod <int>("WriteLine", emit_A >> 1);
            }).Compile();

            ((Action)showResult)();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(8);
                EVar emit_B    = EVar.CreateWithoutTempVar(4);
                method.ExecuteMethod <int>("WriteLine", emit_A << 1);
            }).Compile();

            ((Action)showResult)();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(8);
                EVar emit_B    = EVar.CreateWithoutTempVar(4);
                method.ExecuteMethod <int>("WriteLine", emit_A | emit_B);
            }).Compile();

            ((Action)showResult)();

            showResult = EHandler.CreateMethod <ENull>((il) =>
            {
                EMethod method = typeof(Console);
                EVar emit_A    = EVar.CreateWithoutTempVar(8);
                EVar emit_B    = EVar.CreateWithoutTempVar(4);
                method.ExecuteMethod <int>("WriteLine", emit_A & emit_B);
            }).Compile();

            ((Action)showResult)();
        }