C# 深拷贝的具体代码的封装与调用

先封装下实现方法:
 1  public class DeepClone
 2     {
 3         public static object CopyObject(Object obj)
 4         {
 5             if (obj == null)
 6             {
 7                 return null;
 8             }
 9             Object targetDeepCopyObj;
10             Type targetType = obj.GetType();
11 
12             //zhileixing
13             if (targetType.IsValueType == true)
14             {
15                 targetDeepCopyObj = obj;
16             }
17 
18             //yin yong  lei xing
19             else
20             {
21                 targetDeepCopyObj = System.Activator.CreateInstance(targetType);
22                 System.Reflection.MemberInfo[] memberCollection = obj.GetType().GetMembers();
23 
24                 foreach (System.Reflection.MemberInfo member in memberCollection)
25                 {
26                     if (member.MemberType == System.Reflection.MemberTypes.Field)
27                     {
28                         System.Reflection.FieldInfo field = (System.Reflection.FieldInfo)member;
29                         Object fieldValue = field.GetValue(obj);
30 
31                         if (fieldValue is ICloneable)
32                         {
33                             field.SetValue(targetDeepCopyObj, (fieldValue as ICloneable).Clone());
34                         }
35                         else
36                         {
37                             //di gui
38                             field.SetValue(targetDeepCopyObj, CopyObject(fieldValue));
39                         }
40                     }//copy attribute
41                     else if (member.MemberType == System.Reflection.MemberTypes.Property)
42                     {
43                         System.Reflection.PropertyInfo myproperty = (System.Reflection.PropertyInfo)member;
44 
45                         MethodInfo info = myproperty.GetSetMethod(false);
46 
47                         if (info != null)
48                         {
49                             try
50                             {
51                                 Object propertyValue = myproperty.GetValue(obj, null);
52 
53                                 if (propertyValue is ICloneable)
54                                 {
55                                     myproperty.SetValue(targetDeepCopyObj, (propertyValue as ICloneable).Clone(),null);
56                                 }
57                                 else
58                                 {
59                                     myproperty.SetValue(targetDeepCopyObj, CopyObject(propertyValue), null);
60                                 }
61                             }
62                             catch (System.Exception ex)
63                             {
64 
65                             }
66                         }
67                     }
68                 }
69             }
70             return targetDeepCopyObj;
71         }
72     }
73 }
View Code
具体调用:
1   protected void Page_Load(object sender, EventArgs e)
2         {
3             User user = new User { name="zkj",age=18};
4            Object obj = DeepClone.CopyObject(user);
5         }
6         public class User {
7             public string name { get; set; }
8             public int age { get; set; }
9          }
View Code
个人理解:上述深拷贝的方法主要是利用反射的原理遍历对象中的属性,对于值类型和引用类型条件判断(不知道值类型和引用类型的孩纸,可以先去复习下),如果是引用类型就就直接用clone()方法,否则递归CopyObject(Object obj
),对类型的数据简单复制。这里有深拷贝与浅拷贝的实质区别的深入分析(http://www.cnblogs.com/nliao/archive/2012/11/18/2776114.html
本文源码摘自:http://www.cnblogs.com/fnz0/p/5645527.html
posted @ 2016-07-06 10:57  ~扎克伯格  阅读(387)  评论(0编辑  收藏  举报