Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep Copy of an Object

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?

like image 892
user2023359 Avatar asked Mar 23 '13 06:03

user2023359


2 Answers

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.

like image 166
KyleMit Avatar answered Oct 20 '22 04:10

KyleMit


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

like image 38
nkvu Avatar answered Oct 20 '22 05:10

nkvu