#region 泛型处理
/// <summary>
/// 实体对象赋值转换
/// 参数实体必需包含无参构造函数,否则出错
/// </summary>
/// <typeparam name="T">实体对象</typeparam>
/// <returns></returns>
public static T CopyTo<T>(this T t)
{
Type type = typeof(T);
T obj = (T)Activator.CreateInstance(type, null);
foreach (PropertyInfo p in type.GetProperties())
{
dynamic tt = t.GetType().GetProperty(p.Name).GetValue(t, null);
p.SetValue(obj, tt, null);
}
return obj;
}
#endregion
#region DataTale
/// <summary>
/// 将DataTable转换成实体【DataTale中列数可以小于实体中列数】
/// DataTabele中列名应与实体属性名一一对应
/// </summary>
/// <typeparam name="Entity">实体对象</typeparam>
/// <param name="dt">数据DataTable</param>
/// <returns>实体列表</returns>
public static List<Entity> ToEntity<Entity>(this DataTable dt, bool IsDefault = false)
{
Type type = typeof(Entity);
List<Entity> list = new List<Entity>();
foreach (DataRow r in dt.Rows)
{
Entity obj = (Entity)Activator.CreateInstance(type, null);
foreach (DataColumn c in dt.Columns)
{
try
{
PropertyInfo p = type.GetProperty(c.ColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
if (p == null)
{
continue;
}
object val = r[c.ColumnName];
if (val == DBNull.Value && IsDefault && p.PropertyType.Name.Contains("Nullable"))
{
string strType = p.PropertyType.ToString().Split('[')[1].Replace("]", "");
val = p.PropertyType.IsValueType ? Activator.CreateInstance(Type.GetType(strType)) : null;
}
int enumVal = 0;
if (p.PropertyType.IsEnum && int.TryParse(val.ToString(), out enumVal))
{//枚举赋值处理
val = Enum.ToObject(p.PropertyType, enumVal);
}
if (val == DBNull.Value && !p.PropertyType.Name.Contains("Nullable") && p.PropertyType.IsValueType)//不允许为空的值类型使用默认值
{
string strType = p.PropertyType.ToString().Replace("[", "").Replace("]", "");//得到类型名
val = Activator.CreateInstance(Type.GetType(strType));
}
p.SetValue(obj, Convert.ChangeType(val, p.PropertyType.Name.Contains("Nullable") ? Nullable.GetUnderlyingType(p.PropertyType) : p.PropertyType), null);
}
catch (Exception ex)
{
LogUtil.Log(ex.Message + ex.StackTrace + c.ColumnName, "Entity");
}
}
list.Add(obj);
}
return list;
}
/// <summary>
/// 将DataTable转换成实体【DataTale中列数可以小于实体中列数】
/// DataTabele中列名应与实体属性名一一对应
/// </summary>
/// <typeparam name="Entity">实体对象</typeparam>
/// <param name="dt">数据DataTable</param>
/// <returns>实体列表</returns>
public static Entity Default<Entity>(this DataTable dt, bool IsDefault = false)
{
List<Entity> list = dt.ToEntity<Entity>(IsDefault);
Entity en = default(Entity);
if (list != null && list.Count > 0)
{
en = list[0];
}
return en;
}
public static DataTable ToDataTable<T>(this List<T> list, bool isStoreDB = true)
{
Type tp = typeof(T);
PropertyInfo[] proInfos = tp.GetProperties();
DataTable dt = new DataTable();
foreach (var item in proInfos)
{
dt.Columns.Add(item.Name, item.PropertyType); //添加列明及对应类型
}
foreach (var item in list)
{
DataRow dr = dt.NewRow();
foreach (var proInfo in proInfos)
{
object obj = proInfo.GetValue(item,null);
if (obj == null)
{
continue;
}
if (isStoreDB && proInfo.PropertyType == typeof(DateTime) && Convert.ToDateTime(obj) < Convert.ToDateTime("1753-01-01"))
{
continue;
}
dr[proInfo.Name] = obj;
}
dt.Rows.Add(dr);
}
return dt;
}
#endregion