322 lines
12 KiB
C#
322 lines
12 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 反射帮助类
|
||
/// </summary>
|
||
public class ExtensiveHelper
|
||
{
|
||
|
||
public ExtensiveHelper()
|
||
{ }
|
||
|
||
/// <summary>
|
||
/// 将TSource类型的实体类对象转换为TDest类型的实体类对象
|
||
/// </summary>
|
||
/// <typeparam name="TSource">要转换的实体类对象T类型</typeparam>
|
||
/// <typeparam name="TDest">目标实体类对象T类型</typeparam>
|
||
/// <param name="s">要转换的实体类对象</param>
|
||
/// <returns>TDest目标实体类对象</returns>
|
||
public static TDest Map<TSource, TDest>(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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将TSource类型的实体类泛型List对象转换为TDest类型的泛型List对象
|
||
/// </summary>
|
||
/// <typeparam name="TSource">要转换的实体类对象T类型</typeparam>
|
||
/// <typeparam name="TDest">目标实体类对象T类型</typeparam>
|
||
/// <param name="sList">要转换的泛型List对象</param>
|
||
/// <returns>TDest目标泛型List对象</returns>
|
||
public static List<TDest> Map<TSource, TDest>(List<TSource> sList)
|
||
where TSource : class, new()
|
||
where TDest : class, new()
|
||
{
|
||
List<TDest> list = new List<TDest>();
|
||
|
||
if (null == sList)
|
||
{
|
||
return list;
|
||
}
|
||
|
||
foreach (TSource s in sList)
|
||
{
|
||
list.Add(Map<TSource, TDest>(s));
|
||
}
|
||
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将TSource类型的实体类泛型List对象转换为TDest类型的泛型List对象
|
||
/// </summary>
|
||
/// <typeparam name="TSource">要转换的实体类对象T类型</typeparam>
|
||
/// <typeparam name="TDest">目标实体类对象T类型</typeparam>
|
||
/// <param name="ISList">要转换的泛型List对象</param>
|
||
/// <returns>TDest目标泛型List对象</returns>
|
||
public static IList<TDest> MapI<TSource, TDest>(IList<TSource> ISList)
|
||
where TSource : class, new()
|
||
where TDest : class, new()
|
||
{
|
||
IList<TDest> Ilist = new List<TDest>();
|
||
|
||
if (null == ISList)
|
||
{
|
||
return Ilist;
|
||
}
|
||
|
||
foreach (TSource s in ISList)
|
||
{
|
||
Ilist.Add(Map<TSource, TDest>(s));
|
||
}
|
||
|
||
return Ilist;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 将DataTable数据转换为TDest类型的泛型List对象
|
||
/// </summary>
|
||
/// <typeparam name="TSource">约束DataTabe的行类型T类型</typeparam>
|
||
/// <typeparam name="TDest">目标实体类对象T类型</typeparam>
|
||
/// <param name="tb">DataTabe数据对象</param>
|
||
/// <returns>TDest目标泛型List对象</returns>
|
||
public static List<TDest> Map<TSource, TDest>(DataTable tb)
|
||
where TSource : DataTable
|
||
where TDest : class, new()
|
||
{
|
||
List<TDest> list = new List<TDest>();
|
||
|
||
if (null == tb || tb.Rows.Count == 0)
|
||
{
|
||
return list;
|
||
}
|
||
|
||
foreach (DataRow row in tb.Rows)
|
||
{
|
||
list.Add(Map<DataRow, TDest>(row, tb.Columns));
|
||
}
|
||
|
||
return list;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将DataTable的行转换成TDest类型的泛型List对象
|
||
/// </summary>
|
||
/// <typeparam name="TSource">约束DataTabe的行类型</typeparam>
|
||
/// <typeparam name="TDest">目标实体类对象T类型</typeparam>
|
||
/// <param name="row">DataTabe的行对象</param>
|
||
/// <param name="columns">DataTabe的所有列数据</param>
|
||
/// <returns>TDest目标泛型List对象</returns>
|
||
public static TDest Map<TSource, TDest>(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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将T类型的List列表转换为DataTable
|
||
/// </summary>
|
||
/// <typeparam name="T">T类型</typeparam>
|
||
/// <param name="list">列表</param>
|
||
/// <param name="properties">想要转换的属性名称列表[key值对应Model属性,value值对应Excel显示的列名]</param>
|
||
/// <returns>返回DataTable</returns>
|
||
public static DataTable ConvertListToTable<T>(List<T> list, Dictionary<string, string> properties)
|
||
where T : class
|
||
{
|
||
DataTable dt = new DataTable();
|
||
if (properties.Count > 0 && list.Count > 0)
|
||
{
|
||
Type type = typeof(T);
|
||
foreach (KeyValuePair<string, string> property in properties)
|
||
{
|
||
dt.Columns.Add(new DataColumn(property.Value));
|
||
}
|
||
foreach (T t in list)
|
||
{
|
||
DataRow dr = dt.NewRow();
|
||
int i = 0;
|
||
foreach (KeyValuePair<string, string> 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将T类型的Dictionary字典列表转换为DataTable
|
||
/// </summary>
|
||
/// <typeparam name="D">D类型</typeparam>
|
||
/// <typeparam name="S">S类型</typeparam>
|
||
/// <param name="dic">字典列表</param>
|
||
/// <param name="list">列名列表</param>
|
||
/// <returns>返回DataTable</returns>
|
||
public static DataTable ConvertDictionaryToTable<D, S>(Dictionary<D, S> dic, List<string> 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, S> d in dic)
|
||
{
|
||
DataRow dr = dt.NewRow();
|
||
dr[0] = d.Key.ToString();
|
||
dr[1] = d.Value.ToString();
|
||
dt.Rows.Add(dr);
|
||
}
|
||
}
|
||
return dt;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将T类型的List列表转换为DataTable
|
||
/// </summary>
|
||
/// <typeparam name="T">T类型</typeparam>
|
||
/// <param name="list">列表</param>
|
||
/// <returns>返回DataTable</returns>
|
||
public static DataTable ConvertListToTable<T>(List<T> 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;
|
||
}
|
||
}
|
||
}
|