using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Reflection; using System.Text; namespace MadRunFabric.Common { /// /// 实体转换辅助类 /// public class ModelConvertHelper where T : new() { public static IList ConvertToModel(DataTable dt) { // 定义集合 IList ts = new List(); // 获得此模型的类型 Type type = typeof(T); string tempName = ""; foreach (DataRow dr in dt.Rows) { int i = 0; T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查DataTable是否包含此列 //if (dt.Columns.Contains(tempName)) //{ // 判断此属性是否有Setter if (!pi.CanWrite) continue; object value = dr[i];//object value = dr[tempName]; if (pi.PropertyType.Equals(typeof(System.Int64))) { pi.SetValue(t, Convert.ToInt64(value), null); } else if (pi.PropertyType.Equals(typeof(System.Int32))) { pi.SetValue(t, Convert.ToInt32(value), null); } else if (pi.PropertyType.Equals(typeof(System.Decimal))) { pi.SetValue(t, Convert.ToDecimal(value), null); } else if (pi.PropertyType.Equals(typeof(System.DateTime))) { if (value != DBNull.Value && value.ToString() != "") pi.SetValue(t, Convert.ToDateTime(value), null); else pi.SetValue(t, DateTime.Now, null); } else if (value != DBNull.Value) pi.SetValue(t, value, null); i++; //} } ts.Add(t); } return ts; } // **//// /// 转换IList为List //将IList接口泛型转为List泛型类型 /// /// 指定的集合中泛型的类型 /// 需要转换的IList /// public static List ConvertIListToList(IList gbList) where T : class //静态方法,泛型转换, { if (gbList != null && gbList.Count >= 1) { List list = new List(); for (int i = 0; i < gbList.Count; i++) //将IList中的元素复制到List中 { T temp = gbList[i] as T; if (temp != null) list.Add(temp); } return list; } return null; } /// /// 把DataTable转换成指定类型的List /// /// /// public static IList ConvertDataTableToList(DataTable dt) { // 定义集合 IList ts = new List(); string tempName = ""; foreach (DataRow dr in dt.Rows) { T t = new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } /// /// 把泛型List转换成DataTable /// /// /// public static DataTable ConvertListToDataTable(List list) { DataTable dt = new DataTable(); // 获得此模型的公共属性 PropertyInfo[] propertys = typeof(T).GetProperties(); foreach (PropertyInfo pi in propertys) { // 判断此属性是否有Getter if (!pi.CanRead) continue; dt.Columns.Add(pi.Name, pi.PropertyType); } foreach (T item in list) { propertys = item.GetType().GetProperties(); DataRow newRow = dt.NewRow(); foreach (PropertyInfo pi in propertys) { if (!pi.CanRead) continue; newRow[pi.Name] = pi.GetValue(item); } dt.Rows.Add(newRow); } return dt; } /// /// List集合转DataTable /// /// 实体类型 /// 传入集合 /// 是否存入数据库DateTime字段,date时间范围没事,取出展示不用设置TRUE /// 返回datatable结果 public static DataTable ListToTable(List list, params string[] propertyName) { List propertyNameList = new List(); if (propertyName != null) { propertyNameList.AddRange(propertyName); } DataTable result = new DataTable(); if (list.Count > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { //if (DBNull.Value.Equals(pi.PropertyType)) //{ // // pi.PropertyType = DateTime; //} Type colType = pi.PropertyType; if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>)) { colType = colType.GetGenericArguments()[0]; } result.Columns.Add(pi.Name, colType); //result.Columns.Add(pi.Name, pi.PropertyType); } else { if (propertyNameList.Contains(pi.Name)) { result.Columns.Add(pi.Name, pi.PropertyType); } } } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } else { if (propertyNameList.Contains(pi.Name)) { object obj = pi.GetValue(list[i], null); tempList.Add(obj); } } } object[] array = tempList.ToArray(); result.LoadDataRow(array, true); } } return result; } } }