MemberwisecloneMethod To create a superficial copy, specifically to create a new object, and then copy the non-static fields of the current object to the new object. If the field is of the value type, perform a step-by-step copy on the field. If the field is of reference type, the referenced object is copied but not referenced. Therefore, the original object and its counterparts reference the same object.
To achieve deep replication, We must traverse graphs composed of mutually referenced objects and process the cyclic reference structure. This is undoubtedly very complicated. Fortunately, with the serialization and deserialization mechanisms of. net, an object can be cloned in depth. The principle is very simple. First, serialize the object to the memory stream. At this time, the state of the object referenced by the object is saved to the memory .. Net serialization mechanism will automatically handle the situation of loop reference. Then, status information in the memory stream is deserialized into a new object. This completes the deep replication of an object. In the prototype design mode, clone is critical.
The following code demonstrates this problem:
Using system; using system. io; using system. runtime. serialization. formatters. binary; namespace clonedemo {[serializable] class democlass {public int I = 0; Public int [] iarr = {1, 2, 3}; Public democlass clone1 () // shallow clone {return this. memberwiseclone () as democlass;} public democlass clone2 () // deep clone {memorystream stream = new memorystream (); binaryformatter formatter = new binaryformatter (); formatter. serialize (stream, this); stream. position = 0; return formatter. deserialize (Stream) as democlass;} class program {static void main (string [] ARGs) {democlass A = new democlass ();. I = 10;. iarr = new int [] {8, 9, 10}; democlass B =. clone1 (); democlass c =. clone2 (); // change iarr [0] of object A, resulting in iarr [0] of object B also changing, and C will not change. iarr [0] = 88; console. writeline ("memberwiseclone"); console. writeline (B. i); foreach (VAR item in B. iarr) {console. writeline (item);} console. writeline ("clone2"); console. writeline (C. i); foreach (VAR item in C. iarr) {console. writeline (item);} console. readline ();}}}
Another example is for arrays. arrays in C # are referenced variables. We use Arrays for Demonstration:
Shallow copy:
Using system;
Class shallowcopy: icloneable
{
Public int [] V = {1, 2, 3 };
Public object clone ()
{
Return this. memberwiseclone ();
}
Public void display ()
{
Foreach (int I in V)
Console. Write (I + ",");
Console. writeline ();
}
}
Class Client
{
Public static void main ()
{
Shallowcopy SC1 = new shallowcopy ();
Shallowcopy SC2 = (shallowcopy) sc1.clone ();
Sc1.v [0] = 9;
Sc1.display ();
Sc2.display ();
}
} The shallowcopy object implements a shap copy. Therefore, when SC1 is cloned, its field V is not cloned. As a result, the fields V of SC1 and SC2 point to the same v, therefore, when the V [0] of SC1 is modified, the V [0] of SC2 also changes.
Deep copy:
Using system;
Class deepcopy: icloneable
{
Public int [] V = {1, 2, 3 };
// Default constructor
Public deepcopy ()
{
}
// Private constructor called by the clone method
Private deepcopy (INT [] V)
{
This. V = (INT []) v. Clone ();
}
Public object clone ()
{
// Construct a new deepcopy object. The construction parameter is
// V used in the original object
Return new deepcopy (this. V );
}
Public void display ()
{
Foreach (int I in V)
Console. Write (I + ",");
Console. writeline ();
}
}
Class Client
{
Public static void main ()
{
Deepcopy DC1 = new deepcopy ();
Deepcopy DC2 = (deepcopy) dc1.clone ();
Dc1.v [0] = 9;
Dc1.display ();
Dc2.display ();
}
}
During this cloning, not only the object itself is cloned, but the array fields in the object are cloned together. Therefore, the printed DC1 is different from DC2.
Of course, we can also create a help class for deep copy:
public static class ObjectCopier{ /// <summary> /// Perform a deep Copy of the object. /// </summary> /// <typeparam name="T">The type of object being copied.</typeparam> /// <param name="source">The object instance to copy.</param> /// <returns>The copied object.</returns> public static T Clone<T>(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } }}
// In Windows Phone 7, use the following method for deep replication.
DLL -- system. runtime. serialization needs to be introduced
Public static t deepcopy <t> (t obj) {object retval; using (memorystream MS = new memorystream () {datacontractserializer SER = new datacontractserializer (typeof (t); Ser. writeobject (MS, OBJ); Ms. seek (0, seekorigin. begin); retval = Ser. readobject (MS); Ms. close () ;}return (t) retval ;}