对象的深层副本 [英] Deep Copy of an Object

查看:197
本文介绍了对象的深层副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:

 选项显式
选项严格on

< Serializable>公共类[类]
私有_Name作为字符串
私有_ListOfFields作为新列表(字段)

公共属性名称作为字符串
获取
返回_Name
结束Get
设置(值为字符串)
_Name =值
结束集
结束属性

公共属性ListOfFields作为列表字段)
获取
返回_ListOfFields
结束Get
设置(值作为列表(字段))
_ListOfFields = value
结束设置
结束属性

公共函数Clone()As [Class]
返回DirectCast(Me.MemberwiseClone,[Class])
结束函数

结束类

字段是我自己写的类。



我需要修改Clone()函数来返回深层副本?

解决方案

另外,我可能会命名你的类别的东西不是类)。



如果您想手动执行,您需要按照以下步骤操作:


  1. 确保您的Field类还实现了深层副本 Clone()方法。如果你还没有这样做,那么这可能涉及它的 Clone()方法创建一个类型的新对象,然后根据当前对象填充其每个属性。如果你的 Field 类的属性是其他类/复杂类型(例如你自己创建的类),那么他们也应该实现 Clone code>,并且您应该致电 Clone()以创建新的深度副本

  2. Clone()方法,你将创建一个类型为[Class]的新对象,例如通过调用其构造函数

  3. 将新对象的 Name 属性设置为 Name 您当前对象的属性

  4. 创建一个新的列表(字段),让我们称为listA为例

  5. 迭代当前列表,并将每个列表项的克隆分配给listA。例如:






  _ListOfFields 
listA.Add(item.Clone())
End







  1. 之后,您可以将您的新列表(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:

  1. Ensure that your Field class also implements a deep copy Clone() method. If you haven't done this already, then this would likely involve its Clone() method creating a new object of type Field and then populating each of its properties based on the current object. If your Field class has properties which are other classes/complex types (e.g. classes you have created yourself) then they should also implement Clone() and you should call Clone() on them to create new deep copies
  2. In your Clone() method for the class you would create a new object of type [Class], e.g. by calling its constructor
  3. Set the Name property of the new object to the Name property of your current object
  4. Create a new List(Of Field), let's call it listA for the sake of example
  5. 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


  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆