public class ListHelper
{
#region 赋值转换
///
/// A实体转换成B实体
///
///
///
/// 原始数据对象
/// 目标对象
///
public static K EntityToEntity
{
if (source != null && target != null)
{
PropertyInfo[] propertys1 = source.GetType().GetProperties();// 获得此模型的公共属性
PropertyInfo[] propertys = target.GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (pi.CanWrite && propertys1.Where(ex => ex.Name == pi.Name).Count() > 0)
{
object value = GetProperty(source, pi.Name);
pi.SetValue(target, value, null);
}
}
}
return target;
}
///
/// A集合转换成B集合
///
///
///
/// 源数据对象
/// 赋值目标对象
public static List
{
if (source != null && target != null)
{
foreach (var item in source)
{
var obj = new K();
EntityToEntity(item, obj);
target.Add(obj);
}
return target;
}
return null;
}
#endregion
#region 属性设置 取值
///
/// 实体获取属性值
///
///
///
///
///
public static object GetProperty
{
PropertyInfo[] propertys = source.GetType().GetProperties();// 获得此模型的公共属性
PropertyInfo pi = propertys.Where(ex => ex.Name == Name).FirstOrDefault();
if (pi != null)
{
return pi.GetValue(source, null);
}
return null;
}
///
/// 实体设置赋值
///
///
///
///
///
///
public static bool SetProperty
{
try
{
PropertyInfo[] propertys = source.GetType().GetProperties();// 获得此模型的公共属性
PropertyInfo pi = propertys.Where(ex => ex.Name == Name).FirstOrDefault();
if (pi != null)
{
pi.SetValue(source, value, null);
}
return true;
}
catch
{
return false;
}
}
#endregion
#region List
///
/// List集合转DataTable
///
///
///
///
public static DataTable ListToDataTable
{
var Dt = new DataTable(typeof(T).Name);
PropertyInfo[] propertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in propertyInfo)
{
Type t = GetCoreType(prop.PropertyType);
Dt.Columns.Add(prop.Name, t);
}
foreach (T item in list)
{
var values = new object[propertyInfo.Length];
for (int i = 0; i < propertyInfo.Length; i++)
{
values[i] = propertyInfo[i].GetValue(item, null);
}
Dt.Rows.Add(values);
}
return Dt;
}
///
/// 如果类型为空否则返回类型返回基本类型
///
///
///
private static Type GetCoreType(Type t)
{
var flag = !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));//判断属性是否为空
if (t != null && flag)
{
if (t.IsValueType)//值类型
{
return Nullable.GetUnderlyingType(t);
}
}
return t;
}
#endregion
#region 集合值对比
///
/// 集合差异比较器,比较两个实体集合值是否一样,
/// true 存在差异 false 不存在差异
///
/// 源版本实体集合
/// 当前版本实体集合
///
public static bool ModelComparison
{
if (source.Count != current.Count) { return true; }
for (int i = 0; i < source.Count; i++)
{
bool flag = ModelComparison(source[i], current[i]);
if (flag) { return flag; }
}
return false;
}
///
/// 实体差异比较器,true 存在差异 false 不存在差异
///
/// 源版本实体
/// 当前版本实体
///
public static bool ModelComparison
{
Type t1 = source.GetType();
Type t2 = current.GetType();
PropertyInfo[] property1 = t1.GetProperties();
//排除主键和基础字段
foreach (PropertyInfo p in property1)
{
string name = p.Name;
string value1 = p.GetValue(source, null)?.ToString();
string value2 = t2.GetProperty(name)?.GetValue(current, null)?.ToString();
if (value1 != value2)
{
return true;
}
}
return false;
}
#endregion
}