using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Eson.Utils { /// /// 反射帮助类 /// public class ExtensiveHelper { public ExtensiveHelper() { } /// /// 将TSource类型的实体类对象转换为TDest类型的实体类对象 /// /// 要转换的实体类对象T类型 /// 目标实体类对象T类型 /// 要转换的实体类对象 /// TDest目标实体类对象 public static TDest Map(TSource s) where TSource : class, new() where TDest : class, new() { //定义TDest目标返回对象 TDest oTarget = new TDest(); //获取TSource, TDest的反射类型以及其公共属性 Type typeTSource = typeof(TSource); Type typeTDest = typeof(TDest); PropertyInfo[] propersTSource = typeTSource.GetProperties(); PropertyInfo[] propersTDest = typeTDest.GetProperties(); #region 遍历转换 foreach (PropertyInfo pTDest in propersTDest) { foreach (PropertyInfo pTSource in propersTSource) { //如果TSource, TDest的属性名称相同,则可进行转换 if (pTDest.Name.Equals(pTSource.Name)) { //获取TSource对象该属性的值 object objValue = pTSource.GetValue(s, null); if (null != objValue) { if (!pTDest.PropertyType.IsGenericType) { //已知类型 try { pTDest.SetValue(oTarget, Convert.ChangeType(objValue, pTDest.PropertyType), null); } catch { continue; } } else { //泛型Nullable<>,未知类型 Type genericTypeDefinition = pTDest.PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable<>)) { pTDest.SetValue(oTarget, Convert.ChangeType(objValue, Nullable.GetUnderlyingType(pTDest.PropertyType)), null); } } } break; } } } #endregion return oTarget; } /// /// 将TSource类型的实体类泛型List对象转换为TDest类型的泛型List对象 /// /// 要转换的实体类对象T类型 /// 目标实体类对象T类型 /// 要转换的泛型List对象 /// TDest目标泛型List对象 public static List Map(List sList) where TSource : class, new() where TDest : class, new() { List list = new List(); if (null == sList) { return list; } foreach (TSource s in sList) { list.Add(Map(s)); } return list; } /// /// 将TSource类型的实体类泛型List对象转换为TDest类型的泛型List对象 /// /// 要转换的实体类对象T类型 /// 目标实体类对象T类型 /// 要转换的泛型List对象 /// TDest目标泛型List对象 public static IList MapI(IList ISList) where TSource : class, new() where TDest : class, new() { IList Ilist = new List(); if (null == ISList) { return Ilist; } foreach (TSource s in ISList) { Ilist.Add(Map(s)); } return Ilist; } /// /// 将DataTable数据转换为TDest类型的泛型List对象 /// /// 约束DataTabe的行类型T类型 /// 目标实体类对象T类型 /// DataTabe数据对象 /// TDest目标泛型List对象 public static List Map(DataTable tb) where TSource : DataTable where TDest : class, new() { List list = new List(); if (null == tb || tb.Rows.Count == 0) { return list; } foreach (DataRow row in tb.Rows) { list.Add(Map(row, tb.Columns)); } return list; } /// /// 将DataTable的行转换成TDest类型的泛型List对象 /// /// 约束DataTabe的行类型 /// 目标实体类对象T类型 /// DataTabe的行对象 /// DataTabe的所有列数据 /// TDest目标泛型List对象 public static TDest Map(DataRow row, DataColumnCollection columns) where TSource : DataRow where TDest : class, new() { //定义TDest目标返回对象 TDest oTarget = new TDest(); //获取TDest的反射类型以及其公共属性 Type typeTDest = typeof(TDest); PropertyInfo[] propersTDest = typeTDest.GetProperties(); #region 遍历转换 foreach (PropertyInfo pTDest in propersTDest) { foreach (DataColumn cl in columns) { //如果DataTable的列名, TDest的属性名称相同,则可进行转换 if (pTDest.Name.Equals(cl.ColumnName)) { //获取TSource对象该属性的值 object objValue = row[cl.ColumnName]; if (null != objValue) { if (!pTDest.PropertyType.IsGenericType) { //已知类型 try { pTDest.SetValue(oTarget, Convert.ChangeType(objValue, pTDest.PropertyType), null); } catch { continue; } } else { //泛型Nullable<>,未知类型 Type genericTypeDefinition = pTDest.PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable<>)) { pTDest.SetValue(oTarget, Convert.ChangeType(objValue, Nullable.GetUnderlyingType(pTDest.PropertyType)), null); } } } break; } } } #endregion return oTarget; } /// /// 将T类型的List列表转换为DataTable /// /// T类型 /// 列表 /// 想要转换的属性名称列表[key值对应Model属性,value值对应Excel显示的列名] /// 返回DataTable public static DataTable ConvertListToTable(List list, Dictionary properties) where T : class { DataTable dt = new DataTable(); if (properties.Count > 0 && list.Count > 0) { Type type = typeof(T); foreach (KeyValuePair property in properties) { dt.Columns.Add(new DataColumn(property.Value)); } foreach (T t in list) { DataRow dr = dt.NewRow(); int i = 0; foreach (KeyValuePair property in properties) { PropertyInfo pi = type.GetProperty(property.Key); object obj = pi.GetValue(t, null); dr[i] = obj == null ? "" : obj.ToString(); i++; } dt.Rows.Add(dr); } } return dt; } /// /// 将T类型的Dictionary字典列表转换为DataTable /// /// D类型 /// S类型 /// 字典列表 /// 列名列表 /// 返回DataTable public static DataTable ConvertDictionaryToTable(Dictionary dic, List list) { DataTable dt = new DataTable(); if (dic.Count > 0 && list.Count > 0) { foreach (string columnName in list) { dt.Columns.Add(new DataColumn(columnName)); } foreach (KeyValuePair d in dic) { DataRow dr = dt.NewRow(); dr[0] = d.Key.ToString(); dr[1] = d.Value.ToString(); dt.Rows.Add(dr); } } return dt; } /// /// 将T类型的List列表转换为DataTable /// /// T类型 /// 列表 /// 返回DataTable public static DataTable ConvertListToTable(List list) where T : class { DataTable dt = new DataTable(); Type type = typeof(T); PropertyInfo[] propers = type.GetProperties(); foreach (PropertyInfo p in propers) { dt.Columns.Add(new DataColumn(p.Name)); } foreach (T t in list) { DataRow dr = dt.NewRow(); for (int i = 0; i < propers.Length; i++) { PropertyInfo pi = type.GetProperty(propers[i].Name); object obj = pi.GetValue(t, null); dr[i] = obj == null ? "" : obj.ToString(); } dt.Rows.Add(dr); } return dt; } } }