使用对象映射的方式来赋值,实际上是利用了反射的机制,达到动态生成对象和动态类型转换的目的。
1.映射转换 从T1转换到T2 则根据T2的属性取T1对应属性值
/// <summary>
/// 映射转换 从T1转换到T2 则根据T2的属性取T1对应属性值
/// </summary>
/// <typeparam name="T1">转换前的对象</typeparam>
/// <typeparam name="T2">转换后的对象</typeparam>
/// <param name="fromModel">对象数据</param>
/// <param name="action">回调函数 列如查出的是id 需要显示name情况</param>
/// <returns></returns>
public static T2 TransferData<T1, T2>(T1 fromModel,Action<T1,T2> action=null) where T2 : class
{
if (fromModel == null)
return null;
var t2 = IOC.CreateInstance<T2>();
PropertyInfo[] propertys1 = IOC.GetProperties(typeof(T1));
PropertyInfo[] propertys2 = IOC.GetProperties(typeof(T2));
propertys2.ToList().ForEach(x =>
{
var p1 = propertys1.FirstOrDefault(o => o.Name == x.Name);
if (p1 != null && x.CanWrite)
{
try
{
var value = p1.GetValue(fromModel);
if (x.PropertyType == typeof(string))
{
x.SetValue(t2, value?.ToString());
}
else if (x.PropertyType == typeof(DateTime))
{
var datetime = Convert.ToDateTime(value);
x.SetValue(t2, datetime);
}
else if (x.PropertyType == typeof(int))
{
x.SetValue(t2, value.ChangeType(0));
}
else if (x.PropertyType == typeof(long))
{
x.SetValue(t2, value.ChangeType<long>(0));
}
else if (x.PropertyType == typeof(decimal))
{
x.SetValue(t2, value.ChangeType<decimal>(0));
}
else if (x.PropertyType == typeof(double))
{
x.SetValue(t2, value.ChangeType<double>(0));
}
else if (x.PropertyType == typeof(float))
{
x.SetValue(t2, value.ChangeType<float>(0));
}
else if(x.CanWrite)
{
x.SetValue(t2, value);
}
}
catch (Exception e)
{
IO.Log.WriteLog(e.Message+ ",StackTrace:" + e.StackTrace);
}
}
});
if (action != null)
{
action(fromModel,t2);
}
return t2;
}
2.合并两个对象 从T1往T2合并 如果T2里面的数据是默认类型值 则从T1取数据往T2填充
/// <summary>
/// 合并两个对象 从T1往T2合并 如果T2里面的数据是默认类型值 则从T1取数据往T2填充
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="fromModel"></param>
/// <param name="afterModel"></param>
/// <param name="action"></param>
/// <returns></returns>
public static T2 MergeData<T1, T2>(T1 fromModel,T2 afterModel, Action<T1, T2> action = null) where T2 : class
{
if (fromModel == null)
return afterModel;
if (afterModel == null)
return null;
//var t2 = IOC.CreateInstance<T2>();
PropertyInfo[] propertys1 = IOC.GetProperties(typeof(T1));
PropertyInfo[] propertys2 = IOC.GetProperties(typeof(T2));
propertys2.ToList().ForEach(x =>
{
var p1 = propertys1.FirstOrDefault(o => o.Name == x.Name);
var p2Value = x.GetValue(afterModel);
if (p1 != null)
{
try
{
var p1Value = p1.GetValue(fromModel);
if (x.PropertyType == typeof (string))
{
if (p1Value != null && p1Value.ToString() != "")
x.SetValue(afterModel, p1Value.ToString());
}
else if (x.PropertyType == typeof (DateTime))
{
//var datetime = Convert.ToDateTime(value);
//x.SetValue(t2, datetime);
}
else if (x.PropertyType == typeof (int))
{
if (Convert.ToInt32(p2Value) == 0 && Convert.ToInt32(p1Value) != 0)
x.SetValue(afterModel, p1Value);
}
else if (x.PropertyType == typeof (decimal))
{
if (Convert.ToDecimal(p2Value) == 0 && Convert.ToDecimal(p1Value) != 0)
x.SetValue(afterModel, p1Value);
}
else if (p2Value == null && p1Value != null)
{
x.SetValue(afterModel, p1Value);
}
}
catch
{
//ex.Error();
}
}
});
if (action != null)
{
action(fromModel, afterModel);
}
return afterModel;
}
3.转换一个集合类型数据
/// <summary>
/// 转换一个集合类型数据
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="list">数据源</param>
/// <param name="action">回调函数 列如查出的是id 需要显示name情况</param>
/// <returns></returns>
public static List<T2> TransferData<T1, T2>(IEnumerable<T1> list,Action<T1, T2> action = null) where T2 : class
{
if (list == null)
return null;
var result = new List<T2>();
foreach(var item in list)
{
var model = TransferData<T1, T2>(item,action);
result.Add(model);
}
return result;
}
网友评论