Ejemplo n.º 1
0
        public static DataReaderEntityBuilder <T> CreateBuilder(Type type, IDataRecord dataRecord)
        {
            {
                DataReaderEntityBuilder <T> dynamicBuilder = new DataReaderEntityBuilder <T>();
                DynamicMethod method = new DynamicMethod("DynamicCreateEntity", type,
                                                         new Type[] { typeof(IDataRecord) }, type, true);
                ILGenerator  generator = method.GetILGenerator();
                LocalBuilder result    = generator.DeclareLocal(type);
                generator.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
                generator.Emit(OpCodes.Stloc, result);
                for (int i = 0; i < dataRecord.FieldCount; i++)
                {
                    string       fieldName    = dataRecord.GetName(i);
                    PropertyInfo propertyInfo = type.GetProperty(fieldName);
                    Label        endIfLabel   = generator.DefineLabel();
                    if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
                    {
                        bool isNullable = false;
                        var  underType  = MappingUntilTool.GetUnderType(propertyInfo, ref isNullable);

                        generator.Emit(OpCodes.Ldarg_0);
                        generator.Emit(OpCodes.Ldc_I4, i);
                        generator.Emit(OpCodes.Callvirt, isDBNullMethod);
                        generator.Emit(OpCodes.Brtrue, endIfLabel);
                        generator.Emit(OpCodes.Ldloc, result);
                        generator.Emit(OpCodes.Ldarg_0);
                        generator.Emit(OpCodes.Ldc_I4, i);
                        GeneratorCallMethod(generator, underType, isNullable, propertyInfo, dataRecord.GetDataTypeName(i), fieldName);
                        generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
                        generator.MarkLabel(endIfLabel);
                    }
                }
                generator.Emit(OpCodes.Ldloc, result);
                generator.Emit(OpCodes.Ret);
                dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load));
                return(dynamicBuilder);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reader转成List《T》
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="dr"></param>
        /// <param name="isClose"></param>
        /// <returns></returns>
        internal static List <T> DataReaderToList <T>(Type type, IDataReader dr, string fields, bool isClose = true, bool isTry = true)
        {
            if (type.Name.Contains("KeyValuePair"))
            {
                List <T> strReval = new List <T>();
                FillValueTypeToDictionary(type, dr, strReval);
                return(strReval);
            }
            //值类型
            else if (type.IsValueType || type == MappingUntilTool.StringType)
            {
                List <T> strReval = new List <T>();
                FillValueTypeToDr <T>(type, dr, strReval);
                return(strReval);
            }
            //数组类型
            else if (type.IsArray)
            {
                List <T> strReval = new List <T>();
                FillValueTypeToArray(type, dr, strReval);
                return(strReval);
            }

            var cacheManager = CacheManager <DataReaderEntityBuilder <T> > .GetInstance();

            string key = "DataReaderToList." + fields + type.FullName;
            DataReaderEntityBuilder <T> eblist = null;

            if (cacheManager.ContainsKey(key))
            {
                eblist = cacheManager[key];
            }
            else
            {
                eblist = DataReaderEntityBuilder <T> .CreateBuilder(type, dr);

                cacheManager.Add(key, eblist, cacheManager.Day);
            }
            List <T> list = new List <T>();

            try
            {
                if (dr == null)
                {
                    return(list);
                }
                while (dr.Read())
                {
                    list.Add(eblist.Build(dr));
                }
                if (isClose)
                {
                    dr.Close(); dr.Dispose(); dr = null;
                }
            }
            catch (Exception ex)
            {
                if (isClose)
                {
                    dr.Close(); dr.Dispose(); dr = null;
                }
                throw ex;
            }
            return(list);
        }