/// <summary>
/// 设置实体对象的修改属性
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public void CloneEntityObject(object srcObj, object desObj)
{
if (srcObj.Equals(desObj))
{
return;
}
if (srcObj.GetType() != desObj.GetType())
{
return;
}
System.Reflection.PropertyInfo[] info = srcObj.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo property in info)
{
desObj.GetType().GetProperty(property.Name).SetValue(desObj,
srcObj.GetType().GetProperty(property.Name).GetValue(srcObj, null), null);
}
}
/// 设置实体对象的修改属性
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public void CloneEntityObject(object srcObj, object desObj)
{
if (srcObj.Equals(desObj))
{
return;
}
if (srcObj.GetType() != desObj.GetType())
{
return;
}
System.Reflection.PropertyInfo[] info = srcObj.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo property in info)
{
desObj.GetType().GetProperty(property.Name).SetValue(desObj,
srcObj.GetType().GetProperty(property.Name).GetValue(srcObj, null), null);
}
}
也可以将实体类直接继承ICloneable接口,并用如上方法来实现Clone()方法.
现在有不少架构直接使用自动代码生成器,也可以不使用反射直接每个属性值进行拷贝亦可。