from:https://blog.csdn.net/poxiaohai2011/article/details/27555951
//C# 中利用反射机制拷贝类的字段和属性(拷贝一个类对象的所有东西付给另一个类对象,而不是付给引用地址)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Collections;
namespace SilverlightApplication1
{
/// <summary>
/// 类属性/字段的值复制工具
/// </summary>
public class ClassValueCopier
{
/// <summary>
/// 复制
/// </summary>
/// <param name="destination">目标</param>
/// <param name="source">来源</param>
/// <returns>成功复制的值个数</returns>
public static int Copy(object destination, object source)
{
if (destination == null || source == null)
{
return 0;
}
return Copy(destination, source, source.GetType());
}
/// <summary>
/// 复制
/// </summary>
/// <param name="destination">目标</param>
/// <param name="source">来源</param>
/// <param name="type">复制的属性字段模板</param>
/// <returns>成功复制的值个数</returns>
public static int Copy(object destination, object source, Type type)
{
return Copy(destination, source, type, null);
}
/// <summary>
/// 复制
/// </summary>
/// <param name="destination">目标</param>
/// <param name="source">来源</param>
/// <param name="type">复制的属性字段模板</param>
/// <param name="excludeName">排除下列名称的属性不要复制</param>
/// <returns>成功复制的值个数</returns>
public static int Copy(object destination, object source, Type type, IEnumerable<string> excludeName)
{
if (destination == null || source == null)
{ return 0; }
if (excludeName == null)
{
excludeName = new List<string>();
}
int i = 0;
Type desType = destination.GetType();
foreach (FieldInfo mi in type.GetFields())
{
if (excludeName.Contains(mi.Name))
{ continue; }
try
{
FieldInfo des = desType.GetField(mi.Name);
if (des != null && des.FieldType == mi.FieldType)
{
des.SetValue(destination, mi.GetValue(source)); i++;
}
}
catch
{
}
} foreach (PropertyInfo pi in type.GetProperties())
{
if (excludeName.Contains(pi.Name))
{
continue;
} try
{
PropertyInfo des = desType.GetProperty(pi.Name);
if (des != null && des.PropertyType == pi.PropertyType && des.CanWrite && pi.CanRead)
{
des.SetValue(destination, pi.GetValue(source, null), null);
i++;
}
}
catch
{
//throw ex;
}
} return i;
}
}
#region 扩展方法 For .NET 3.0+
/// <summary>
/// 类属性/字段的值复制工具 扩展方法
/// </summary>
public static class ClassValueCopier2
{
/// <summary>
/// 获取实体类的属性名称
/// </summary>
/// <param name="source">实体类</param>
/// <returns>属性名称列表</returns>
public static List<string> GetPropertyNames(this object source)
{
if (source == null)
{
return new List<string>();
} return GetPropertyNames(source.GetType());
}
/// <summary>
/// 获取类类型的属性名称(按声明顺序)
/// </summary>
/// <param name="source">类类型</param>
/// <returns>属性名称列表</returns>
public static List<string> GetPropertyNames(this Type source)
{
return GetPropertyNames(source, true);
}
/// <summary>
/// 获取类类型的属性名称
/// </summary>
/// <param name="source">类类型</param>
/// <param name="declarationOrder">是否按声明顺序排序</param>
/// <returns>属性名称列表</returns>
public static List<string> GetPropertyNames(this Type source, bool declarationOrder)
{
if (source == null)
{
return new List<string>();
}
var list = source.GetProperties().AsQueryable();
if (declarationOrder)
{
list = list.OrderBy(p => p.MetadataToken);
} return list.Select(o => o.Name).ToList(); ;
}
/// <summary>
/// 从源对象赋值到当前对象
/// </summary>
/// <param name="destination">当前对象</param>
/// <param name="source">源对象</param>
/// <returns>成功复制的值个数</returns>
public static int CopyValueFrom(this object destination, object source)
{
return CopyValueFrom(destination, source, null);
}
/// <summary>
/// 从源对象赋值到当前对象
/// </summary>
/// <param name="destination">当前对象</param>
/// <param name="source">源对象</param>
/// <param name="excludeName">排除下列名称的属性不要复制</param>
/// <returns>成功复制的值个数</returns>
public static int CopyValueFrom(this object destination, object source, IEnumerable<string> excludeName)
{
if (destination == null || source == null)
{
return 0;
}
return ClassValueCopier.Copy(destination, source, source.GetType(), excludeName);
}
/// <summary>
/// 从当前对象赋值到目标对象
/// </summary>
/// <param name="source">当前对象</param>
/// <param name="destination">目标对象</param>
/// <returns>成功复制的值个数</returns>
public static int CopyValueTo(this object source, object destination)
{
return CopyValueTo(destination, source, null);
}
/// <summary>
/// 从当前对象赋值到目标对象
/// </summary>
/// <param name="source">当前对象</param>
/// <param name="destination">目标对象</param>
/// <param name="excludeName">排除下列名称的属性不要复制</param>
/// <returns>成功复制的值个数</returns>
public static int CopyValueTo(this object source, object destination, IEnumerable<string> excludeName)
{
if (destination == null || source == null)
{
return 0;
}
return ClassValueCopier.Copy(destination, source, source.GetType(), excludeName);
}
}
#endregion
}