DataTable转换成匿名类的List类型
因为匿名类是不能够 Activator.CreateInstance进行反射实例化的
/// <summary> /// 匿名类的转换方式 /// </summary> /// <param name="GenericType"></param> /// <param name="dataTable"></param> /// <returns></returns> public static IList FromTable(Type GenericType, DataTable dataTable) { Type typeMaster = typeof(List<>); Type listType = typeMaster.MakeGenericType(GenericType); IList list = Activator.CreateInstance(listType) as IList; if (dataTable == null || dataTable.Rows.Count == 0) return list; var constructor = GenericType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .OrderBy(c => c.GetParameters().Length).First(); var parameters = constructor.GetParameters(); var values = new object[parameters.Length]; foreach (DataRow dr in dataTable.Rows) { int index = 0; foreach (ParameterInfo item in parameters) { object itemValue = null; if (dr[item.Name] != null && dr[item.Name] != DBNull.Value) { itemValue = Convert.ChangeType(dr[item.Name], item.ParameterType.GetUnderlyingType()); } values[index++] = itemValue; } list.Add(constructor.Invoke(values)); } return list; } /// <summary> /// 匿名类的转换方式 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataTable"></param> /// <returns></returns> public static List<T> FromTable<T>(DataTable dataTable) { List<T> list = new List<T>(); if (dataTable == null || dataTable.Rows.Count == 0) return list; //取当前匿名类的构造函数 var constructor = typeof(T).GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) .OrderBy(c => c.GetParameters().Length).First(); //取当前构造函数的参数 var parameters = constructor.GetParameters(); var values = new object[parameters.Length]; foreach (DataRow dr in dataTable.Rows) { int index = 0; foreach (ParameterInfo item in parameters) { object itemValue = null; if (dr[item.Name] != null) { itemValue = Convert.ChangeType(dr[item.Name], item.ParameterType.GetUnderlyingType()); } values[index++] = itemValue; } T entity = (T)constructor.Invoke(values); list.Add(entity); } return list; }
使用方法
var entity = new { Name = "item", ID = 0, GuidType = Guid.Empty }; DataTable dataTable = new DataTable(); dataTable.Columns.Add("Name", typeof(string)); dataTable.Columns.Add("ID", typeof(int)); dataTable.Columns.Add("GuidType", typeof(Guid)); for (int i = 0; i < 10; i++) { DataRow dr = dataTable.NewRow(); dr["Name"] = "STRING" + i; dr["ID"] = i; if (i % 2 == 0) dr["GuidType"] = Guid.Empty; else { dr["GuidType"] = DBNull.Value; } dataTable.Rows.Add(dr); } IList list = EntityExtensions.FromTable(entity.GetType(), dataTable);
千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs