Object.MemberwiseClone 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
创建当前 Object的浅表副本。
protected:
System::Object ^ MemberwiseClone();
protected object MemberwiseClone ();
member this.MemberwiseClone : unit -> obj
Protected Function MemberwiseClone () As Object
返回
当前 Object的浅表副本。
示例
以下示例演示了 MemberwiseClone 方法。 它定义一个 ShallowCopy
方法,该方法调用 MemberwiseClone 方法对 Person
对象执行浅复制操作。 它还定义对 Person
对象执行深层复制操作的 DeepCopy
方法。
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)MemberwiseClone();
}
public Person DeepCopy()
{
Person other = (Person)MemberwiseClone();
other.IdInfo = new IdInfo(IdInfo.IdNumber);
return other;
}
}
public class Example
{
public static void Main()
{
// Create an instance of Person and assign values to its fields.
Person p1 = new()
{
Age = 42,
Name = "Sam",
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.Age = 39;
p1.Name = "George";
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: {p.Name:s}, Age: {p.Age:d}");
Console.WriteLine($" Value: {p.IdInfo.IdNumber:d}");
}
}
/* The example displays the following output:
*
* Original values of p1 and p2:
p1 instance values:
Name: Sam, Age: 42
Value: 6565
p2 instance values:
Name: Sam, Age: 42
Value: 6565
Values of p1 and p2 after changes to p1:
p1 instance values:
Name: Frank, Age: 32
Value: 7878
p2 instance values:
Name: Sam, Age: 42
Value: 7878
Values of p1 and p3 after changes to p1:
p1 instance values:
Name: George, Age: 39
Value: 8641
p3 instance values:
Name: Frank, Age: 32
Value: 7878
*/
open System
type IdInfo(IdNumber) =
member val IdNumber = IdNumber with get, set
type Person() =
[<DefaultValue>]
val mutable public Age: int
[<DefaultValue>]
val mutable public Name: string
[<DefaultValue>]
val mutable public IdInfo: IdInfo
member this.ShallowCopy() =
this.MemberwiseClone() :?> Person
member this.DeepCopy() =
let other = this.MemberwiseClone() :?> Person
other.IdInfo <- IdInfo this.IdInfo.IdNumber
other
let displayValues (p: Person) =
printfn $" Name: {p.Name:s}, Age: {p.Age:d}"
printfn $" Value: {p.IdInfo.IdNumber:d}"
// Create an instance of Person and assign values to its fields.
let p1 = Person()
p1.Age <- 42
p1.Name <- "Sam"
p1.IdInfo <- IdInfo 6565
// Perform a shallow copy of p1 and assign it to p2.
let p2 = p1.ShallowCopy()
// Display values of p1, p2
printfn "Original values of p1 and p2:"
printfn " p1 instance values: "
displayValues p1
printfn " 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
printfn "\nValues of p1 and p2 after changes to p1:"
printfn " p1 instance values: "
displayValues p1
printfn " p2 instance values:"
displayValues p2
// Make a deep copy of p1 and assign it to p3.
let p3 = p1.DeepCopy()
// Change the members of the p1 class to new values to show the deep copy.
p1.Age <- 39
p1.Name <- "George"
p1.IdInfo.IdNumber <- 8641
printfn "\nValues of p1 and p3 after changes to p1:"
printfn " p1 instance values: "
displayValues p1
printfn " p3 instance values:"
displayValues p3
// The example displays the following output:
// Original values of p1 and p2:
// p1 instance values:
// Name: Sam, Age: 42
// Value: 6565
// p2 instance values:
// Name: Sam, Age: 42
// Value: 6565
//
// Values of p1 and p2 after changes to p1:
// p1 instance values:
// Name: Frank, Age: 32
// Value: 7878
// p2 instance values:
// Name: Sam, Age: 42
// Value: 7878
//
// Values of p1 and p3 after changes to p1:
// p1 instance values:
// Name: George, Age: 39
// Value: 8641
// p3 instance values:
// Name: Frank, Age: 32
// Value: 7878
Public Class IdInfo
Public IdNumber As Integer
Public Sub New(IdNumber As Integer)
Me.IdNumber = IdNumber
End Sub
End Class
Public Class Person
Public Age As Integer
Public Name As String
Public IdInfo As IdInfo
Public Function ShallowCopy() As Person
Return DirectCast(MemberwiseClone(), Person)
End Function
Public Function DeepCopy() As Person
Dim other As Person = DirectCast(MemberwiseClone(), Person)
other.IdInfo = New IdInfo(IdInfo.IdNumber)
Return other
End Function
End Class
Module Example
Public Sub Main()
' Create an instance of Person and assign values to its fields.
Dim p1 As New Person()
p1.Age = 42
p1.Name = "Sam"
p1.IdInfo = New IdInfo(6565)
' Perform a shallow copy of p1 and assign it to p2.
Dim p2 As Person = 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)
Console.WriteLine()
' 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("Values of p1 and p2 after changes to p1:")
Console.WriteLine(" p1 instance values: ")
DisplayValues(p1)
Console.WriteLine(" p2 instance values:")
DisplayValues(p2)
Console.WriteLine()
' Make a deep copy of p1 and assign it to p3.
Dim p3 As Person = p1.DeepCopy()
' Change the members of the p1 class to new values to show the deep copy.
p1.Age = 39
p1.Name = "George"
p1.IdInfo.IdNumber = 8641
Console.WriteLine("Values of p1 and p3 after changes to p1:")
Console.WriteLine(" p1 instance values: ")
DisplayValues(p1)
Console.WriteLine(" p3 instance values:")
DisplayValues(p3)
End Sub
Public Sub DisplayValues(p As Person)
Console.WriteLine(" Name: {0:s}, Age: {1:d}", p.Name, p.Age)
Console.WriteLine(" Value: {0:d}", p.IdInfo.IdNumber)
End Sub
End Module
' The example displays the following output:
' Original values of m1 and m2:
' m1 instance values:
' Name: Sam, Age: 42
' Value: 6565
' m2 instance values:
' Name: Sam, Age: 42
' Value: 6565
'
' Values of m1 and m2 after changes to m1:
' m1 instance values:
' Name: Frank, Age: 32
' Value: 7878
' m2 instance values:
' Name: Sam, Age: 42
' Value: 7878
'
' Values of m1 and m3 after changes to m1:
' m1 instance values:
' Name: George, Age: 39
' Value: 8641
' m3 instance values:
' Name: Frank, Age: 32
' Value: 7878
在此示例中,Person.IdInfo
属性返回 IdInfo
对象。 如示例中的输出所示,通过调用 MemberwiseClone 方法克隆 Person
对象时,克隆的 Person
对象是原始对象的独立副本,只是它们共享相同的 Person.IdInfo
对象引用。 因此,修改克隆的 Person.IdInfo
属性会更改原始对象的 Person.IdInfo
属性。 另一方面,执行深层复制操作时,克隆的 Person
对象(包括其 Person.IdInfo
属性)可以修改,而不会影响原始对象。
注解
MemberwiseClone 方法通过创建新对象来创建浅表副本,然后将当前对象的非静态字段复制到新对象。 如果字段是值类型,则执行字段的逐位副本。 如果字段是引用类型,则复制引用,但引用对象不是;因此,原始对象及其克隆引用同一对象。
为了说明浅表复制操作与深层复制操作之间的差异,请考虑一个名为 X 的对象,该对象又引用对象 A 和 B。对象 B 又引用对象 C。X 的浅表副本创建也引用对象 A 和 B 的新对象 X2。相比之下,X 的深层副本会创建一个新对象 X2,该对象引用新对象 A2 和 B2(即 A 和 B.B2 的副本)引用新的对象 C2,该对象是 C 的副本。
如果 MemberwiseClone 方法执行的浅表复制操作不满足你的需求,则可通过多种方式实现深层复制操作。 其中包括:
- 调用要复制的对象的类构造函数,以创建具有从第一个对象获取的属性值的第二个对象。 这假定对象的值完全由其类构造函数定义。
- 调用 MemberwiseClone 方法创建对象的浅表副本,然后将其值与原始对象相同的新对象分配给其值为引用类型的任何属性或字段。 示例中的
DeepCopy
方法演示了此方法。 - 序列化要深度复制的对象,然后将序列化的数据还原到其他对象变量。
- 将反射与递归配合使用来执行深层复制操作。