예제 #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();
        }
예제 #2
0
 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)());
     }
 }
예제 #3
0
        /// <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);
        }
예제 #4
0
        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("??");
            }
        }
예제 #5
0
        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)();
        }
예제 #6
0
        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)();
        }
예제 #7
0
        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)());
        }
예제 #8
0
        static void Main(string[] args)
        {
            EArray ea = new EArray();

            ea.Add(0);
            ea.Add(1);
            ea.Add(2);
            ea.Add(3);
            ea.Add(4);
            ea.Add(5);
            ea.Add(6);
            ea.Add(7);
            ea.Add(8);
            ea.Add(9);

            //ea.PrintArray();

            ea.Delete(5);

            ea.PrintArray();

            Console.ReadLine();
        }
예제 #9
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)();
        }
예제 #10
0
파일: EClone.cs 프로젝트: wcfylcf/Natasha
        /// <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);
        }