克隆对象的方法实例教程

发布时间:2022-05-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了克隆对象的方法实例教程脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
  克隆对象在开发过程中经常会遇到,有些时候需要浅克隆,有些时候需要深克隆,具体它们之间有什么区别,以及实现方式有哪些,在这里总结一下。

  实现深克隆有以下几种方法。

手动

代码如下:

//手动复制
VAR user2 = new User
{
	Id = user1.Id,
	Name = new UserName 
	{
		FirstName= user1.Name.FirstName,
		LastName= user1.Name.LastName
	}
};

反射

代码如下:

1 //反射2 var user3 = user1.Copy() as User;

扩展方法:

 1 public static class DeepCopyHelPEr 2 { 3     public static object Copy(this object obj) 4     { 5         Object targetDeepCopyObj; 6         Type targetType = obj.GetType(); 7         //值类型 8         if (targetType.IsValueType == true) 9         {10             targetDeepCopyObj = obj;11         }12         //引用类型 13         else14         {15             targetDeepCopyObj = System.Activator.Createinstance(targetType);   //创建引用对象 16             System.Reflection.MemberInfo[] memberCollection = obj.GetType().GetMembers();17 18             foreach (System.Reflection.MemberInfo member in memberCollection)19             {20                 if (member.MemberType == System.Reflection.MemberTypes.Field)21                 {22                     System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)member;23                     Object fieldValue = field.GetValue(obj);24                     if (fieldValue is ICloneable)25                     {26                         field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone());27                     }28                     else29                     {30                         field.SetValue(targetDeepCopyObj, Copy(fieldValue));31                     }32 33                 }34                 else if (member.MemberType == System.Reflection.MemberTypes.PRoperty)35                 {36                     System.Reflection.PropertyInfo myProperty = (System.Reflection.PropertyInfo)member;37                     MethodInfo info = myProperty.GetSetMethod(false);38                     if (info != null)39                     {40                         object propertyValue = myProperty.GetValue(obj, null);41                         if (propertyValue is ICloneable)42                         {43                             myProperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(), null);44                         }45                         else46                         {47                             myProperty.SetValue(targetDeepCopyObj, Copy(propertyValue), null);48                         }49                     }50 51                 }52             }53         }54         return targetDeepCopyObj;55     }56 }
View Code

序列化

代码如下:

1 //序列化2 var user4 = user1.DeepClone();

扩展方法:

 1 /// <summary> 2 /// 深克隆 3 /// 先序列化再反序列化 4 /// </summary> 5 /// <typeparam name="T"></typeparam> 6 /// <param name="obj"></param> 7 /// <returns></returns> 8 public static T DeepClone<T>(this T obj) where T : class 9 {10     return obj != null ? obj.ToJson().FromJson<T>() : null;11 }
View Code

其它还有使用表达式。

总结:

  1. 手动复制性能最好,但是遇到很复杂的类的时候,工作量很大。

  2. 反射和序列化比起来,序列化更简单

以上就是克隆对象的方法实例教程的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的克隆对象的方法实例教程全部内容,希望文章能够帮你解决克隆对象的方法实例教程所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。