예제 #1
0
        public static void TestList()
        {
            List <string> list = new List <string>();

            bool shut = list.All(func);

            Console.WriteLine(shut);

            Func <List <string>, Func <string, bool>, bool> ShowDelegate = (Func <List <string>, Func <string, bool>, bool>)EHandler.CreateMethod <List <string>, Func <string, bool>, bool>((il) =>
            {
                EVar elist = EVar.CreateVarFromParameter(0);
                EVar efunc = EVar.CreateVarFromParameter(1);
                EMethod.Load(elist)
                .Use(typeof(Enumerable))
                .AddGenricType <string>()
                .ExecuteMethod <Func <string, bool> >("All", efunc);
            }).Compile();

            Console.WriteLine(ShowDelegate(list, (str) =>
            {
                if (str == "1")
                {
                    return(true);
                }
                return(false);
            }));

            Action ShowDelegate1 = (Action)EHandler.CreateMethod <ENull>((il) =>
            {
                EModel model = EModel.CreateModel <Student>().UseDefaultConstructor();
                EMethod.Load(model).ExecuteMethod("Show");
                EMethod.Load(model).ExecuteMethod <string>("Show", "test");
            }).Compile();

            ((Action)ShowDelegate1)();
        }
예제 #2
0
        /// <summary>
        /// 获取Reader映射缓存方法
        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="reader">数据库返回的DataReader</param>
        /// <param name="sql">SQL语句</param>
        /// <returns>动态缓存方法</returns>
        private static SqlDelegate <T> .GetReaderInstance GetEmitReaderCache <T>(IDataReader reader, int startIndex, int length)
        {
            Type returnType = typeof(T);

            if (!Cache.SqlCache.ContainsKey(returnType))
            {
                ModelAnalyser.Initialization(returnType);
            }
            SqlModel sqlModel = Cache.SqlCache[returnType];

            Delegate dynamicMethod = EHandler.CreateMethod <IDataReader, T>((il) =>
            {
                EMethod dataHandler = typeof(IDataRecord);
                EVar parameterVar   = EVar.CreateVarFromParameter <IDataRecord>(0);

                if (returnType == typeof(object) || returnType == typeof(string) || returnType == typeof(byte[]) || (returnType.IsValueType && returnType.IsPrimitive) || il.IsNullable(returnType))
                {
                    if (returnType.IsValueType && returnType.IsPrimitive)
                    {
                        LoadStrongTypeValue(returnType, parameterVar, startIndex);
                    }
                    else
                    {
                        EJudge.If(() => { EMethod.Load(parameterVar).ExecuteMethod <int>("IsDBNull", startIndex); })(() =>
                        {
                            if (il.IsNullable(returnType))
                            {
                                EModel model = EModel.CreateModel(returnType).UseDefaultConstructor();
                                model.Load();
                            }
                            else
                            {
                                ENull.LoadNull();
                            }
                        }).Else(() =>
                        {
                            LoadStrongTypeValue(returnType, parameterVar, startIndex);
                        });
                    }
                }
                else
                {
                    EModel model = EModel.CreateModel <T>().UseDefaultConstructor();

                    for (int i = startIndex; i < startIndex + length; i += 1)
                    {
                        string tempName = sqlModel.GetRealName(reader.GetName(i));
                        Type type       = null;
                        if (!model.Struction.Properties.ContainsKey(tempName) && !model.Struction.Fields.ContainsKey(tempName))
                        {
                            continue;
                        }
                        else
                        {
                            type = sqlModel.Struction.ModelTypeCache[tempName];
                        }
                        if (type.IsValueType && type.IsPrimitive)
                        {
                            model.Set(tempName, () =>
                            {
                                LoadStrongTypeValue(type, parameterVar, i);
                            });
                        }
                        else
                        {
                            EJudge.IfTrue(() => { EMethod.Load(parameterVar).ExecuteMethod <int>("IsDBNull", i); })(() =>
                            {
                                model.Set(tempName, () =>
                                {
                                    LoadStrongTypeValue(type, parameterVar, i);
                                });
                            });
                        }
                    }
                    model.Load();
                }
            }).Compile(typeof(SqlDelegate <T> .GetReaderInstance));

            return((SqlDelegate <T> .GetReaderInstance)dynamicMethod);
        }