Deep Copy of an Object

随声附和 提交于 2019-12-01 17:21:59

(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.

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 use BinaryFormatter.Serialize

If you want to make your own class expose the clonable method itself, you can add the method and implement the ICloneable interface like this:

<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 controversies as 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!