使用對(duì)象映射的方式來賦值,實(shí)際上是利用了反射的機(jī)制,達(dá)到動(dòng)態(tài)生成對(duì)象和動(dòng)態(tài)類型轉(zhuǎn)換的目的。
1.映射轉(zhuǎn)換 從T1轉(zhuǎn)換到T2 則根據(jù)T2的屬性取T1對(duì)應(yīng)屬性值
/// <summary>
/// 映射轉(zhuǎn)換 從T1轉(zhuǎn)換到T2 則根據(jù)T2的屬性取T1對(duì)應(yīng)屬性值
/// </summary>
/// <typeparam name="T1">轉(zhuǎn)換前的對(duì)象</typeparam>
/// <typeparam name="T2">轉(zhuǎn)換后的對(duì)象</typeparam>
/// <param name="fromModel">對(duì)象數(shù)據(jù)</param>
/// <param name="action">回調(diào)函數(shù) 列如查出的是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.合并兩個(gè)對(duì)象 從T1往T2合并 如果T2里面的數(shù)據(jù)是默認(rèn)類型值 則從T1取數(shù)據(jù)往T2填充
/// <summary>
/// 合并兩個(gè)對(duì)象 從T1往T2合并 如果T2里面的數(shù)據(jù)是默認(rèn)類型值 則從T1取數(shù)據(jù)往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.轉(zhuǎn)換一個(gè)集合類型數(shù)據(jù)
/// <summary>
/// 轉(zhuǎn)換一個(gè)集合類型數(shù)據(jù)
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="list">數(shù)據(jù)源</param>
/// <param name="action">回調(diào)函數(shù) 列如查出的是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;
}