. NET-based deep replication and shortest Replication
Here is an example to illustrate the difference between simple replication and deep replication: copy the code public class Content {public int Val ;} public class Cloner {public Content MyContent = new Content (); public Cloner (int newVal) {MyContent. val = newVal;} public object GetCopy () {return MemberwiseClone () ;}} here we have two classes, one Content class, and only one Val for the value type int, another class is a Cloner class, which has a member of the Content type, then a constructor can initialize the member, and finally a GetCopy method, which is used to copy itself through the MemberwiseClone method. The following code calls the Cloner class: copy the code static void Main (string [] args) {Cloner source = new Cloner (10); Cloner target = (Cloner) source. getCopy (); // The returned Object type requires type conversion. console. writeLine ("target. myContent. val = {0} ", target. myContent. val); source. myContent. val = 15; Console. writeLine ("target. myContent. val = {0} ", target. myContent. val); Console. readKey ();} the result of copying the code is: Copy. We can see that we use the GetCopy () function to Copy the source class to target, However, when we change the source, the value of the source output also changes. From this we can conclude that MemberwiseClone () copies only references, that is, the MyContent of source and target is the same object instance. This is simple replication. How can we implement deep replication? In the. NET Framework, we are provided with the ICloneable interface. First, let's take a look at the ICloneable interface: Copy code // Abstract: // supports cloning, that is, using the same value as the existing instance to create a new instance of the class. [ComVisible (true)] public interface ICloneable {// Abstract: // create a new object as a copy of the current instance. //// Return result: // as a new object of this instance copy. Object Clone () ;}copy the code in the above example, we only need to modify some code: copy the code public class Cloner: ICloneable {public Content MyContent = new Content (); public Cloner (int newVal) {MyContent. val = newVal;} // public object GetCopy () // {// return MemberwiseClone (); //} public object Clone () {Cloner cloned = new Cloner (MyContent. val); return cloned ;}} copy the Code. In order to make a difference, I put the previous code on it. The annotated code is a simple copy, followed by a deep copy, the difference is that the MyContent of this instance is used. val The test result is: QQ20141116144216. Here, if the MyContent member is not a value type, we still need to perform the depth, as shown below: public object Clone () {Cloner cloned = new Cloner (); cloned. myContent = MyContent. clone (); return cloned;} This is the case for deep replication and shortest replication. The key is to create a new object instance and return it instead of returning the original object instance. Do you understand?