Esempio n. 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();
        }
Esempio n. 2
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)();
        }
Esempio n. 3
0
        /// <summary>
        /// 深度复制对象,并压入栈中,创建一个新的临时变量
        /// </summary>
        /// <param name="value">类或者结构体或者数组</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static LocalBuilder GetCloneBuilder(object value, Type type)
        {
            ILGenerator il = ThreadCache.GetIL();

            if (il.IsNullable(type))
            {
                LocalBuilder builder = il.DeclareLocal(type);
                if (value == null)
                {
                    il.REmit(OpCodes.Ldloca_S, builder);
                    il.InitObject(type);
                }
                else
                {
                    il.LoadObject(value, value.GetType());
                    il.CallNullableCtor(type);
                }
                return(builder);
            }
            else if (value == null)
            {
                LocalBuilder builder = il.DeclareLocal(type);
                il.REmit(OpCodes.Ldnull);
                il.REmit(OpCodes.Stloc_S, builder.LocalIndex);
                return(builder);
            }
            else if (type.IsValueType && !type.IsPrimitive)
            {
                LocalBuilder builder = il.DeclareLocal(type);
                if (type.IsEnum)
                {
                    il.EmitInt((int)value);
                    il.REmit(OpCodes.Stloc_S, builder);
                    return(builder);
                }
                if (EStruckCheck.IsDefaultStruct(value, type))
                {
                    EModel structModel = EModel.CreateModel(type).UseDefaultConstructor();
                    return(structModel.Builder);
                }
            }
            else if (type.IsArray)
            {
                Array  tempArray    = (Array)value;
                Type   instanceType = value.GetType().GetElementType();
                EArray array        = EArray.CreateArraySpecifiedLength(value.GetType(), tempArray.Length);
                for (int i = 0; i < tempArray.Length; i += 1)
                {
                    object result = tempArray.GetValue(i);

                    if (result != null)
                    {
                        if (array.IsStruct && !array.ElementType.IsEnum)
                        {
                            if (EStruckCheck.IsDefaultStruct(result, instanceType))
                            {
                                continue;
                            }
                        }
                        array.StoreArray(i, result);
                    }
                }
                return(array.Builder);
            }
            if (!EReflector.GetMethodDict.ContainsKey(type))
            {
                EReflector.Create(type);
            }

            Dictionary <string, GetterDelegate> GetDict = EReflector.GetMethodDict[type];
            ClassStruction struction = ClassCache.ClassInfoDict[type.Name];
            EModel         model     = EModel.CreateModel(type).UseDefaultConstructor();

            //foreach (var item in struction.Properties)
            //{
            //    MethodInfo info = item.Value.GetSetMethod(true);
            //    if (info == null || GetDict[item.Key] == null || info.IsPrivate || info.IsStatic)
            //    {
            //        continue;
            //    }

            //    object result = GetDict[item.Key](value);
            //    if (result == null)
            //    {
            //        continue;
            //    }
            //    model.SProperty(item.Key, result);
            //}

            foreach (var item in struction.Fields)
            {
                if (item.Value.IsStatic)
                {
                    continue;
                }
                object result = GetDict[item.Key](value);
                if (result == null)
                {
                    continue;
                }
                model.SField(item.Key, result);
            }
            return(model.Builder);
        }