Esempio n. 1
0
        /// <summary>
        /// datatable转List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List <T> ToList <T>(this DataTable dt) where T : class, new()
        {
            var list = new List <T>();

            if (dt == null || dt.Rows.Count == 0)
            {
                return(list);
            }

            list.AddRange(dt.Rows.Cast <DataRow>().Select(info => DataTableBuilder <T> .CreateBuilder(dt.Rows[0]).Build(info)));
            return(list);
        }
Esempio n. 2
0
        /// <summary>
        /// datatable转List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List <T> ToList <T>(this DataTable dt) where T : class, new()
        {
            List <T> list = new List <T>();

            using (dt)
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return(list);
                }

                DataTableBuilder <T> eblist = DataTableBuilder <T> .CreateBuilder(dt.Rows[0]);

                foreach (DataRow info in dt.Rows)
                {
                    list.Add(eblist.Build(info));
                }

                return(list);
            }
        }
Esempio n. 3
0
        public static DataTableBuilder <T> CreateBuilder(DataRow dataRecord)
        {
            DataTableBuilder <T> dynamicBuilder = new DataTableBuilder <T>();
            DynamicMethod        method         = new DynamicMethod("DynamicCreateEntity", typeof(T), new Type[]
            {
                typeof(DataRow)
            }, typeof(T), true);
            ILGenerator  generator = method.GetILGenerator();
            LocalBuilder result    = generator.DeclareLocal(typeof(T));

            generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
            generator.Emit(OpCodes.Stloc, result);
            for (int i = 0; i < dataRecord.ItemArray.Length; i++)
            {
                PropertyInfo propertyInfo = typeof(T).GetProperty(dataRecord.Table.Columns[i].ColumnName);
                Label        endIfLabel   = generator.DefineLabel();
                if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
                {
                    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);
                    generator.Emit(OpCodes.Callvirt, GetValueMethod);
                    generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                    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);
        }