C # like MemberwiseClone copy and Clone (shades Clone)

MemberwiseClone  method creates a shallow copy, specifically, is to create a new object, and then copy the non-static fields of the current object to the new object. If the field is a value type, then the field is performed bit by bit copy. If the field is a reference type, then copy references but does not copy the objects referenced; therefore, a copy of the original object and reference the same object.

In order to achieve a deep copy, we must have each traverse FIG objects referenced configuration, and processing cycles which require a reference structure. This is undoubtedly very complex. Fortunately, by means of serialization and de-serialization mechanism .Net can be very simple depth Clone an object. The principle is simple, the first object serialization stream into memory, which is the object and object-object references the state are saved to memory. .Net serialization mechanism automatically handling of circular references. Memory status information is then deserialize the stream to a new object. Such a deep copy of the object is complete. In the prototype design mode CLONE technology is critical.

The following code demonstrates the 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() //浅CLONE
        {
            return this.MemberwiseClone() as DemoClass;
        }

        public DemoClass Clone2() //深clone
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);
            = 0 stream.Position; 
            return formatter.Deserialize (Stream) AS DemoClass; 
        } 
    } 

    class Program 
    { 
        static void the Main (String [] args) 
        { 
            DemoClass new new DemoClass A = (); 
            AI = 10; 
            a.iArr = new new int [ ] {. 8,. 9, 10}; 
            DemoClass a.Clone1 b = (); 
            DemoClass a.Clone2 C = (); 

            // change a target iArr [0], b resulting in the object iArr [0] has changed and c does not change 
              a.iArr [0] = 88; 

            Console.WriteLine ( "MemberwiseClone"); 
            Console.WriteLine (BI); 
            the foreach (Item in b.iArr var) 
            {
                Console.WriteLine (Item); 
            } 

            Console.WriteLine ( "clone2"); 
            Console.WriteLine (CI); 
            the foreach (Item in c.iArr var) 
            { 
                Console.WriteLine (Item); 
            } 

            Console.ReadLine (); 
        } 
    } 
} surrogate micro 13,802,269,370    surrogate electric 13,802,269,370    Beijing surrogate electric 13,802,269,370   Shanghai surrogate micro 13,802,269,370

Guess you like

Origin www.cnblogs.com/bbc2020/p/12460549.html