vb.net 对象的深拷贝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15584053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deep Copy of an Object
提问by user2023359
Can I please have some help to perform a deep copy of an object.
我可以请一些帮助来执行对象的深层复制。
Here is my code:
这是我的代码:
Option Explicit On
Option Strict On
<Serializable> Public Class [Class]
Private _Name As String
Private _ListOfFields As New List(Of Field)
Public Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
Public Property ListOfFields As List(Of Field)
Get
Return _ListOfFields
End Get
Set(value As List(Of Field))
_ListOfFields = value
End Set
End Property
Public Function Clone() As [Class]
Return DirectCast(Me.MemberwiseClone, [Class])
End Function
End Class
Field is a Class that I have written myself as well.
Field 也是我自己写的一个类。
What do I need to modify for the Clone() Function to return a deep copy?
我需要修改什么才能让 Clone() 函数返回深拷贝?
回答by KyleMit
You can create a clone of any class by calling this helper function:
您可以通过调用此辅助函数来创建任何类的克隆:
Function DeepClone(Of T)(ByRef orig As T) As T
' Don't serialize a null object, simply return the default for that object
If (Object.ReferenceEquals(orig, Nothing)) Then Return Nothing
Dim formatter As New BinaryFormatter()
Dim stream As New MemoryStream()
formatter.Serialize(stream, orig)
stream.Seek(0, SeekOrigin.Begin)
Return CType(formatter.Deserialize(stream), T)
End Function
This works by serializing all the information from your class into a portable object and then rewriting it in order to sever any reference pointers.
这是通过将类中的所有信息序列化为可移植对象然后重写它以切断任何引用指针来工作的。
Note: The passed in class and any other classes it exposes as properties must be marked
<Serializable()>in order to useBinaryFormatter.Serialize
注意:必须标记传入的类和它作为属性公开的任何其他类
<Serializable()>才能使用BinaryFormatter.Serialize
If you want to make your own class expose the clonable method itself, you can add the method and implement the ICloneableinterface like this:
如果你想让你自己的类公开可克隆方法本身,你可以添加方法并实现ICloneable接口,如下所示:
<Serializable()>
Public Class MyClass : Implements ICloneable
'NOTE - The Account class must also be Serializable
Public Property PersonAccount as Account
Public Property FirstName As String
Function Clone(ByRef orig As MyClass) As MyClass Implements ICloneable.Clone
' Don't serialize a null object, simply return the default for that object
If (Object.ReferenceEquals(orig, Nothing)) Then Return Nothing
Dim formatter As New BinaryFormatter()
Dim stream As New MemoryStream()
formatter.Serialize(stream, orig)
stream.Seek(0, SeekOrigin.Begin)
Return CType(formatter.Deserialize(stream), T)
End Function
End Class
Note: Be aware ICloneable comes with it's share of controversiesas it does not indicate to the caller if it is performing a deep or shallow clone. In reality, you don't need the interface to be able to add the method to your class.
注意:请注意 ICloneable 带来了一些争议,因为它不会向调用者表明它是执行深克隆还是浅克隆。实际上,您不需要接口就可以将方法添加到您的类中。
回答by nkvu
(As an aside, I probably would name your class something other than "Class").
(顺便说一句,我可能会给你的班级命名而不是“班级”)。
If you wanted to do it all by hand you would need to follow steps like:
如果您想手动完成所有操作,则需要执行以下步骤:
- Ensure that your Field class also implements a deep copy
Clone()method. If you haven't done this already, then this would likely involve itsClone()method creating a new object of typeFieldand then populating each of its properties based on the current object. If yourFieldclass has properties which are other classes/complex types (e.g. classes you have created yourself) then they should also implementClone()and you should callClone()on them to create new deep copies - In your
Clone()method for the class you would create a new object of type [Class], e.g. by calling its constructor - Set the
Nameproperty of the new object to theNameproperty of your current object - Create a new
List(Of Field), let's call it listA for the sake of example - Iterate over your current list and assign a clone of each list item to listA. For example:
- 确保您的 Field 类也实现了深度复制
Clone()方法。如果您还没有这样做,那么这可能涉及它的Clone()方法创建一个新的类型对象,Field然后根据当前对象填充它的每个属性。如果您的Field类具有其他类/复杂类型的属性(例如您自己创建的类),那么它们也应该实现Clone()并且您应该调用Clone()它们来创建新的深度副本 - 在
Clone()类的方法中,您将创建一个 [Class] 类型的新对象,例如通过调用其构造函数 - 将
Name新对象的Name属性设置为当前对象的属性 - 创建一个 new
List(Of Field),为了举例,我们称其为 listA - 迭代当前列表并将每个列表项的副本分配给 listA。例如:
For Each item in _ListOfFields
listA.Add(item.Clone())
End
- After that you can assign your new list (listA) to the object you have created in the
Clone()method
- 之后,您可以将新列表 (listA) 分配给您在
Clone()方法中创建的对象
There is an alternative (probably better) by-hand approach that is in VB.NET described here.
在此处描述的 VB.NET 中有一种替代(可能更好)的手工方法。
If you wanted to cheat a bit then you could just serialize your existing object and then deserialize it into a new object like the technique here
如果您想作弊,那么您可以序列化现有对象,然后将其反序列化为新对象,例如此处的技术
I would say the serialize then deserialize technique is the "easiest" one.
我会说序列化然后反序列化技术是“最简单”的技术。

