using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;
namespace System.Common.Helpers
{
///
/// 实体转换辅助类
///
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)
{
if (i >= dr.ItemArray.Length)
break;
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.FullName.Contains(typeof(System.Int64).FullName))
{ pi.SetValue(t, Convert.ToInt64(value), null); }
else if (pi.PropertyType.FullName.Contains(typeof(System.Int32).FullName))
{ pi.SetValue(t, Convert.ToInt32(value), null); }
else if (pi.PropertyType.FullName.Contains(typeof(System.Decimal).FullName))
{ pi.SetValue(t, Convert.ToDecimal(value), null); }
else if (pi.PropertyType.FullName.Contains(typeof(System.DateTime).FullName))
{
if (value != DBNull.Value && value.ToString() != "")
pi.SetValue(t, Convert.ToDateTime(value), null);
else
pi.SetValue(t, null, 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;
}
}
public class ModelConvertHelper where T : new() where T1 : new()
{
///
/// 实体相同属性快速赋值转换
///
///
///
/// 返回第一个参数
public static T ModeToModel(T t1, T1 t2)
{
PropertyInfo[] t1pList = typeof(T).GetProperties();
PropertyInfo[] t2pList = typeof(T1).GetProperties();
foreach (PropertyInfo t2p in t2pList)
{
object t2value = t2p.GetValue(t2);
foreach (var t1p in t1pList)
{
if (t1p.Name == t2p.Name)
{
t1p.SetValue(t1, t2value, null);
}
}
}
return t1;
}
}
}