浅谈C#中的深拷贝(DeepCopy)与浅拷贝(MemberwiseClone)

时间:2022-11-19 19:53:15

场景:MVVM模式中数据双向绑定,想实现编辑、保存、撤销操作时也双向绑定了,不能实现撤销操作。

后来通过搜索找到了继承IEditableObject接口实现撤销操作,但是对其中使用了MemberwiseClone还不是很了解。

 

MemberwiseClone:

https://msdn.microsoft.com/zh-cn/library/system.object.memberwiseclone(v=vs.110).aspx

MemberwiseClone方法创建的新对象,然后将当前对象的非静态字段复制到新的对象创建的浅表副本。 

如果字段是值类型,则执行字段的按位复制。 

如果字段是引用类型,引用将复制,但被引用的对象不会被复制。因此,原始对象和其克隆引用同一对象。

笔记:修改克隆对象会影响原对象的引用类型。

 

DeepCopy:

In a deep copy, all objects are duplicated; in a shallow copy, only the top-level objects are duplicated and the lower levels contain references.

在深拷贝中,会复制所有对象。在浅拷贝中,仅对*对象进行了复制,对低级别对象进行了引用。

笔记:深拷贝中,修改克隆对象不会影响原对象。

 

附上代码,其中Persona类实现了浅拷贝和深拷贝的方法。

深拷贝额外复制了其中自定义的类IdInfo。

using System;

public class IdInfo
{
    public int IdNumber;

    public IdInfo(int IdNumber)
    {
        this.IdNumber = IdNumber;
    }
}

public class Person 
{
    public int Age;
    public string Name;
    public IdInfo IdInfo;

    public Person ShallowCopy()
    {
       return (Person) this.MemberwiseClone();
    }

    public Person DeepCopy()
    {
       Person other = (Person) this.MemberwiseClone();
       other.IdInfo = new IdInfo(IdInfo.IdNumber);
       other.Name = String.Copy(Name);
       return other;
    }
}

public class Example
{
    public static void Main()
    {
        // Create an instance of Person and assign values to its fields.
        Person p1 = new Person();
        p1.Age = 42;
        p1.Name = "Sam";
        p1.IdInfo = new IdInfo(6565);

        // Perform a shallow copy of p1 and assign it to p2.
        Person p2 = p1.ShallowCopy();

        // Display values of p1, p2
        Console.WriteLine("Original values of p1 and p2:");
        Console.WriteLine("   p1 instance values: ");
        DisplayValues(p1);
        Console.WriteLine("   p2 instance values:");
        DisplayValues(p2);

        // Change the value of p1 properties and display the values of p1 and p2.
        p1.Age = 32;
        p1.Name = "Frank";
        p1.IdInfo.IdNumber = 7878;
        Console.WriteLine("\nValues of p1 and p2 after changes to p1:");
        Console.WriteLine("   p1 instance values: ");
        DisplayValues(p1);
        Console.WriteLine("   p2 instance values:");
        DisplayValues(p2);

        // Make a deep copy of p1 and assign it to p3.
        Person p3 = p1.DeepCopy();
        // Change the members of the p1 class to new values to show the deep copy.
        p1.Name = "George";
        p1.Age = 39;
        p1.IdInfo.IdNumber = 8641;
        Console.WriteLine("\nValues of p1 and p3 after changes to p1:");
        Console.WriteLine("   p1 instance values: ");
        DisplayValues(p1);
        Console.WriteLine("   p3 instance values:");
        DisplayValues(p3);
    }

    public static void DisplayValues(Person p)
    {
        Console.WriteLine("      Name: {0:s}, Age: {1:d}", p.Name, p.Age);
        Console.WriteLine("      Value: {0:d}", p.IdInfo.IdNumber);
    }
}