Example #1
0
        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)();
        }
Example #2
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);
        }
Example #3
0
        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))));
        }
Example #4
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)();
        }