对象的深层副本 [英] Deep Copy of an Object
问题描述
这是我的代码:
选项显式
选项严格on
< Serializable>公共类[类]
私有_Name作为字符串
私有_ListOfFields作为新列表(字段)
公共属性名称作为字符串
获取
返回_Name
结束Get
设置(值为字符串)
_Name =值
结束集
结束属性
公共属性ListOfFields作为列表字段)
获取
返回_ListOfFields
结束Get
设置(值作为列表(字段))
_ListOfFields = value
结束设置
结束属性
公共函数Clone()As [Class]
返回DirectCast(Me.MemberwiseClone,[Class])
结束函数
结束类
字段是我自己写的类。
我需要修改Clone()函数来返回深层副本?
另外,我可能会命名你的类别的东西不是类)。
如果您想手动执行,您需要按照以下步骤操作:
- 确保您的Field类还实现了深层副本
Clone()
方法。如果你还没有这样做,那么这可能涉及它的Clone()
方法创建一个类型的新对象
,然后根据当前对象填充其每个属性。如果你的Field
类的属性是其他类/复杂类型(例如你自己创建的类),那么他们也应该实现Clone code>,并且您应该致电
Clone()
以创建新的深度副本 - 在
Clone()
方法,你将创建一个类型为[Class]的新对象,例如通过调用其构造函数 - 将新对象的
Name
属性设置为Name
您当前对象的属性 - 创建一个新的
列表(字段)
,让我们称为listA为例 - 迭代当前列表,并将每个列表项的克隆分配给listA。例如:
_ListOfFields
listA.Add(item.Clone())
End
- 之后,您可以将您的新列表(listA)分配给您在
Clone()
method
有一种替代方法(可能更好),在VB.NET中描述在这里。
如果您想欺骗一点,那么你可以只是序列化你现有的对象,然后反序列化成一个新的对象,如技术这里
我会说序列化然后反序列化技术是最简单的。
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.
What do I need to modify for the Clone() Function to return a deep copy?
(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 typeField
and then populating each of its properties based on the current object. If yourField
class 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
Name
property of the new object to theName
property 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:
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
There is an alternative (probably better) by-hand approach that is in VB.NET described here.
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.
这篇关于对象的深层副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!