/// <summary> /// 表转实体 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="table"></param> /// <returns></returns> public static List <TEntity> ConvertToEntityList <TEntity>(System.Data.DataTable dataTable) { List <TEntity> list = new List <TEntity>(); if (dataTable == null || dataTable.Rows.Count == 0) { return(list); } // 创建行转换器 TableEntityBuilder <TEntity> builder = TableEntityBuilder <TEntity> .CreateBuilder(dataTable); // 遍历每一行进行转换 foreach (DataRow dr in dataTable.Rows) { list.Add(builder.Build(dr)); } return(list); }
public static TableEntityBuilder <T> CreateBuilder(DataTable table)//, PropertyInfo[] props) { PropertyInfo[] props = typeof(T).GetProperties(); DataTable parentTable = table; TableEntityBuilder <T> dynamicBuilder = new TableEntityBuilder <T>(); //定义一个名为DynamicCreate的动态方法,返回值typof(T),参数typeof(DataRow) DynamicMethod method = new DynamicMethod("DynamicCreate", typeof(T), new Type[] { typeof(DataRow) }, typeof(T), true); ILGenerator generator = method.GetILGenerator(); //创建一个MSIL生成器,为动态方法生成代码 LocalBuilder result = generator.DeclareLocal(typeof(T)); //声明指定类型的局部变量 可以T t;这么理解 //T t=new T() 构造实体实例 generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes)); generator.Emit(OpCodes.Stloc, result); for (int i = 0; i < parentTable.Columns.Count; i++) { PropertyInfo pInfo = null; string fieldName = string.Empty; foreach (PropertyInfo p in props) { EntityAttribute[] attrs = (EntityAttribute[])p.GetCustomAttributes(typeof(EntityAttribute), true); if (attrs.Length > 0 && attrs[0].FieldName.ToLower() == parentTable.Columns[i].ColumnName.ToLower()) { pInfo = p; fieldName = attrs[0].FieldName; break; } else if (p.Name.ToLower() == parentTable.Columns[i].ColumnName.ToLower()) { pInfo = p; fieldName = p.Name; break; } } System.Reflection.Emit.Label endIfLabel = generator.DefineLabel(); //实体存在该属性 且该属性有SetMethod方法 if (pInfo != null && pInfo.GetSetMethod() != null) { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldc_I4, i); generator.Emit(OpCodes.Callvirt, isDBNullMethod);//调用IsDBNull方法 如果IsDBNull==true contine generator.Emit(OpCodes.Brtrue, endIfLabel); /*If the value in the data reader is not null, the code sets the value on the object.*/ generator.Emit(OpCodes.Ldloc, result); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldstr, fieldName); generator.Emit(OpCodes.Callvirt, getValueMethod);//调用get_Item方法 Type memberType = pInfo.PropertyType; Type nullUnderlyingType = Nullable.GetUnderlyingType(memberType); Type unboxType = nullUnderlyingType != null ? nullUnderlyingType : memberType; if (unboxType == typeof(byte[]) || unboxType == typeof(string)) { generator.Emit(OpCodes.Castclass, memberType); } else { if (unboxType == typeof(int) || unboxType == typeof(double) || unboxType == typeof(decimal))// || unboxType == typeof(decimal?)) { generator.Emit(OpCodes.Call, ConvertMethods[unboxType]); } else { generator.Emit(OpCodes.Unbox_Any, parentTable.Columns[i].DataType); //if (nullUnderlyingType != null) //{ // generator.Emit(OpCodes.Newobj, memberType.GetConstructor(new[] { nullUnderlyingType })); //} } if (nullUnderlyingType != null) { generator.Emit(OpCodes.Newobj, memberType.GetConstructor(new[] { nullUnderlyingType })); } } generator.Emit(OpCodes.Callvirt, pInfo.GetSetMethod());//给该属性设置对应值 generator.MarkLabel(endIfLabel); } } generator.Emit(OpCodes.Ldloc, result); //加载返回结果到 generator.Emit(OpCodes.Ret); //方法结束,返回 //完成动态方法的创建,并且创建执行该动态方法的委托,赋值到全局变量handler,handler在Build方法里Invoke dynamicBuilder.handler = (Load)method.CreateDelegate(typeof(Load)); return(dynamicBuilder); }