Prerequisites: copying objects by value from one variable to another, rather than copying objects by reference (that is, copying objects in the same way as the structure), can be very complicated. Because an object may contain references of many objects, such as fields and members, this involves many annoying processing operations. Copying each member from one object to another may not succeed, because some members may be of the reference type.
Light replication: simply copying objects by members can be derived from System. the MemberwiseClone () method of the Object is implemented. This is a protected method, but it is easy to define a public method to call this method on the Object. The replication function of this method becomes light replication.
The advantage of light replication: you do not need to reference the object type.
Shortest method:
View sourceprint? 01 using System;
02 using System. Collections. Generic;
03 using System. Text;
04
05 namespace leleapplication2
06 {
07 public class Content
08 {
09 public int Val;
10}
11
12 public class Cloner
13 {
14 public Content MyContent = new Content ();
15 public Cloner (int newVal)
16 {
17 MyContent. Val = newVal;
18}
19 public object GetCopy ()
20 {
21 return MemberwiseClone ();
22}
23}
24
25 public class Program
26 {
27 static void Main (string [] args)
28 {
29 Cloner mySource = new Cloner (5 );
30 Cloner myTarget = (Cloner) mySource. GetCopy ();
31 Console. WriteLine ("MyTarget. MyContent. val = {0}", myTarget. MyContent. Val );
32 mySource. MyContent. Val = 2;
33 Console. WriteLine ("MyTarget. MyContent. val = {0}", myTarget. MyContent. Val );
34 Console. ReadKey ();
35}
36}
37}
Here mySource. myContent. val = 2 changes the value of the public field val, so the generated result is as follows: MyTarget. myContent. val = 5MyTarget. myContent. val = 2 the above uses the replication reference type to demonstrate how to use the replication. If you want to make the generated value 5, you need to use deep replication.
About deep replication:
To use deep replication, you must make the class implement the ICloneable interface. This interface has a method Clone (). This method returns an object type, its signature, and the GetCopy () method used above without parameters () the method is the same. 1 using System; 2 using System. collections. generic; 3 using System. text; 4 5 namespace ConsoleApplication2 6 {7 public class Content 8 {9 public int Val; 10 11} 12 13 public class Cloner: ICloneable14 {15 public Content MyContent = new Content (); 16 17 public Cloner (int newVal) 18 {19 MyContent. val = newVal; 20} 21 public object GetCopy () 22 {23 return MemberwiseClone (); 24} 25 26 # region ICloneable member 27 28 public object Clone () 29 {30 Cloner clonedCloner = new Cloner (MyContent. val); 31 return clonedCloner; 32} 33 34 # endregion35} 36 37 public class Program38 {39 static void Main (string [] args) 40 {41 Cloner mySource = new Cloner (5); 42 Cloner myTarget = (Cloner) mySource. clone (); 43 Console. writeLine ("MyTarget. myContent. val = {0} ", myTarget. myContent. val); 44 mySource. myContent. val = 2; 45 Console. writeLine ("MyTarget. myContent. val = {0} ", myTarget. myContent. val); 46 Console. readKey (); 47} 48} 49}
Because the Cloner implements ICloneable and uses deep replication to create a new object that acts as a copy of the current instance (clonedCloner here,
Finally, in the test of deep replication, the Clone () method is called, but the GetCopy () method is not called, which causes myTarget. myContent. the Val value is always new Cloner (5), rather than the last defined value 2.
After learning this knowledge point, I think that the ICloneable () interface exists to call the clone () method. If you need to copy a value of a reference type or value type to an object,
Let it directly implement the method of this interface. This may not be understood correctly. I hope you can communicate with me.
Description of the Clone () method of the ICloneable () interface:
Clone can be implemented either as a deep copy or as a superficial copy. In a deep copy, all objects are repeated. In a superficial copy, only top-level objects are repeated, and objects below the top-level include references.
The clone result must be of the same type as the original Instance or be compatible with the original Instance.