Exemple #1
0
        /// <summary>
        /// DataRow转实体
        /// </summary>
        /// <param name="dataRecord">DataRow</param>
        /// <returns>DataRow转实体</returns>
        public static DataTableEntityBuilder <Entity> CreateBuilder(DataRow dataRecord)
        {
            DataTableEntityBuilder <Entity> dynamicBuilder = new DataTableEntityBuilder <Entity>();
            DynamicMethod method    = new DynamicMethod("DataTableDynamicCreateEntity", typeof(Entity), new Type[] { typeof(DataRow) }, typeof(Entity), true);
            ILGenerator   generator = method.GetILGenerator();
            LocalBuilder  result    = generator.DeclareLocal(typeof(Entity));

            generator.Emit(OpCodes.Newobj, typeof(Entity).GetConstructor(Type.EmptyTypes));
            generator.Emit(OpCodes.Stloc, result);

            for (int i = 0; i < dataRecord.ItemArray.Length; i++)
            {
                PropertyInfo propertyInfo = typeof(Entity).GetProperty(dataRecord.Table.Columns[i].ColumnName);
                Label        endIfLabel   = generator.DefineLabel();
                if (propertyInfo.IsNotNull() && propertyInfo.GetSetMethod().IsNotNull())
                {
                    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);
        }
        /// <summary>
        /// 最快 DataTable To 实体列表 IL
        /// </summary>
        /// <example>
        /// <code>
        /// Data.GetDataTable("Select MemberID,RealName from UC_Member").ToList&lt;UC_Member>();
        /// </code>
        /// </example>
        /// <typeparam name="TResult">返回值类型</typeparam>
        /// <param name="dt">DataTable</param>
        /// <returns>实体列表</returns>
        public static List <TResult> ToList <TResult>(this DataTable dt) where TResult : class, new()
        {
            List <TResult> list = new List <TResult>();

            if (dt.IsNull() || dt.Rows.Count == 0)
            {
                return(list);
            }
            DataTableEntityBuilder <TResult> eblist = DataTableEntityBuilder <TResult> .CreateBuilder(dt.Rows[0]);

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