Implementing deep copy of objects in C ා

Posted by thangappan on Thu, 13 Feb 2020 19:34:42 +0100

Deep copy refers to copying a reference type (including the reference types in the type) to a copy (completely two objects in memory, without any reference relationship)...... direct code:

 1     /// <summary>
 2     /// Deep copy of object (serialization)
 3     /// </summary>
 4     public static class MyDeepCopy
 5     {
 6 
 7         /// <summary>
 8         /// xml Deep copy by serialization
 9         /// </summary>
10         /// <typeparam name="T"></typeparam>
11         /// <param name="t"></param>
12         /// <returns></returns>
13         public static T XmlDeepCopy<T>(T t)
14         {
15             //Establish Xml serialized objects 
16             XmlSerializer xml = new XmlSerializer(typeof(T));
17             using (MemoryStream ms = new MemoryStream())//Create memory stream
18             {
19                 //Serialize objects into memory
20                 xml.Serialize(ms, t);
21                 ms.Position = default;//Set memory stream location to 0
22                 return (T)xml.Deserialize(ms);//Continue deserialization
23             }
24         }
25 
26         /// <summary>
27         /// Deep copy by binary serialization
28         /// Ensure that all members of the class to be copied have been marked as [Serializable] If this feature is not added, a special error is reported
29         /// </summary>
30         /// <typeparam name="T"></typeparam>
31         /// <param name="t"></param>
32         /// <returns></returns>
33         public static T BinaryDeepCopy<T>(T t)
34         {
35             //Create a binary serialized object
36             BinaryFormatter bf = new BinaryFormatter();
37             using (MemoryStream ms = new MemoryStream())//Create memory stream
38             {
39                 //Serialize objects into memory
40                 bf.Serialize(ms, t);
41                 ms.Position = default;//Set memory stream location to 0
42                 return (T)bf.Deserialize(ms);//Continue deserialization
43             }
44         }
45     }

Shallow copy refers to copying the value type in the type, but the reference type in the type is not copied, so it is very simple to realize shallow copy. Then write a method in the class, and call its own memberwise clone() method internally to get an object type object, and force it to the current object to return, as shown in the following table:

 1     public class Person
 2     {
 3         public int ID { get; set; }
 4         public string Name { get; set; }
 5         public string Email { get; set; }
 6         public Cat Cat { get; set; }
 7 
 8         /// <summary>
 9         /// Person Shallow copy of class
10         /// </summary>
11         /// <returns></returns>
12         public Person QainClone()
13         {
14             //Calling the native shallow copy method gets a objec Type of object, and then forced to return directly
15             return MemberwiseClone() as Person;
16         }
17     }

string is out of column

Topics: C# xml