using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WX.CRM.Common
{
public class CopyInofHelper
{
///
/// 类型转换
///
/// 原类型
/// 目标类型
///
///
public static R ToResult(T obj) where R : new()
{
R t = new R();
var props = typeof(T).GetProperties().Where(a => a.CanRead && a.CanWrite);
foreach (var item in props)
{
item.SetValue(t, item.GetValue(obj, null), null);
}
return t;
}
///
/// 从父类到子类转换
///
/// 父类
/// 子类
///
///
public static TC CopyFromParent(TP parent) where TC : TP, new() where TP : new()
{
if (parent == null) return default(TC);
var child = new TC();
typeof(TP).GetProperties().Where(p => p.CanRead && p.CanWrite).ToList()
.ForEach(p => p.SetValue(child, p.GetValue(parent, null), null));
return child;
}
///
/// 从子类到父类
///
/// 父类
/// 子类
///
///
public static TP CopyToParent(TC child) where TC : TP, new() where TP : new()
{
if (child == null) return default(TP);
var parent = new TP();
typeof(TP).GetProperties().Where(p => p.CanRead && p.CanWrite).ToList()
.ForEach(p => p.SetValue(parent, p.GetValue(child, null), null));
return parent;
}
}
}