对象属性拷贝(全匹配拷贝)
/// <summary>
/// 对象属性拷贝(全匹配拷贝)
/// </summary>
/// <param name="obj1">源对象</param>
/// <param name="obj2">目标对象</param>
/// <returns>目标对象</returns>
public static T PropertyCopy<K, T>(K obj1, ref T obj2)
{
Type souType = obj1.GetType();
Type tarType = obj2.GetType();
PropertyInfo[] pis = souType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
if (null != pis)
{
foreach (PropertyInfo pi in pis)
{
string propertyName = pi.Name;
PropertyInfo pit = tarType.GetProperty(propertyName);
if (pit != null)
{
pit.SetValue(obj2, pi.GetValue(obj1, null), null);
}
}
}
return (T)obj2;
}