title: ICloneable deepcopy of an object in .net
body: <p>I'm trying to add a deep-copy method using ICloneable to classes that have been auto-generated from an xsd using xsd.exe. I can get it to work on a simple level but as soon as the objects become nested then the clone method doesn't work.</p>
  
  <p>I'm pretty sure i've got the clone method wrong on the DirectorReturnType class but I can't work out how to fix it.</p>
  
  <p>Can anyone offer any assistance? I've attached the subs and classes below:</p>
  
  <pre><code>        Dim oDirRetType As New DirectorReturnType
          Dim oDirPerType As New DirectorPersonType
  
          Dim DirPerTypeT1 As New DirectorPersonType
          Dim DirPerTypeT2 As New DirectorPersonType
  
          Dim DirRetTypeT1 As New DirectorReturnType
          Dim DirRetTypeT2 As New DirectorReturnType
  
          Dim AROT1 As New AnnualReturnOfficer
          Dim AROT2 As New AnnualReturnOfficer
  </code></pre>
  
  <p>This works as expected and messages "test1" and then "test2":</p>
  
  <pre><code>        'Works
          oDirPerType.Occupation = "test1"
          DirRetTypeT1.Item = oDirPerType.Clone
  
          oDirPerType.Occupation = "test2"
          DirRetTypeT2.Item = oDirPerType.Clone
  
          DirPerTypeT1 = DirRetTypeT1.Item
          DirPerTypeT2 = DirRetTypeT2.Item
  
          MsgBox(DirPerTypeT1.Occupation)
          MsgBox(DirPerTypeT2.Occupation)
  </code></pre>
  
  <p>If I then add an extra object AROTx of type AnnualRetunOfficer then it messages "Test2" then "Test2".           </p>
  
  <pre><code>        'Doesnt Work
          oDirPerType.Occupation = "test1"
          oDirRetType.Item = oDirPerType
          AROT1.Item = oDirRetType.Clone
  
          oDirPerType.Occupation = "test2"
          DirRetTypeT2.Item = oDirPerType
          AROT2.Item = oDirRetType.Clone
  
          DirRetTypeT1 = AROT1.Item
          DirPerTypeT1 = DirRetTypeT1.Item
  
          DirRetTypeT2 = AROT2.Item
          DirPerTypeT2 = DirRetTypeT2.Item
  
          MsgBox(DirPerTypeT1.Occupation)
          MsgBox(DirPerTypeT2.Occupation)
  </code></pre>
  
  <p>DirectorPersonType:</p>
  
  <pre><code>Partial Public Class DirectorPersonType
  
  Inherits PersonBaseType
  
  Implements ICloneable
  
  Private serviceAddressField As ServiceAddressType
  
  Private dOBField As Date
  
  Private nationalityField As String
  
  Private occupationField As String
  
  Private countryOfResidenceField As String
  
  Private previousNamesField() As PreviousNameType
  
  '''&lt;remarks/&gt;
  Public Property ServiceAddress() As ServiceAddressType
      Get
          Return Me.serviceAddressField
      End Get
      Set(value As ServiceAddressType)
          Me.serviceAddressField = value
      End Set
  End Property
  
  '''&lt;remarks/&gt;
  &lt;System.Xml.Serialization.XmlElementAttribute(DataType:="date")&gt; _
  Public Property DOB() As Date
      Get
          Return Me.dOBField
      End Get
      Set(value As Date)
          Me.dOBField = value
      End Set
  End Property
  
  '''&lt;remarks/&gt;
  Public Property Nationality() As String
      Get
          Return Me.nationalityField
      End Get
      Set(value As String)
          Me.nationalityField = value
      End Set
  End Property
  
  '''&lt;remarks/&gt;
  Public Property Occupation() As String
      Get
          Return Me.occupationField
      End Get
      Set(value As String)
          Me.occupationField = value
      End Set
  End Property
  
  '''&lt;remarks/&gt;
  Public Property CountryOfResidence() As String
      Get
          Return Me.countryOfResidenceField
      End Get
      Set(value As String)
          Me.countryOfResidenceField = value
      End Set
  End Property
  
  '''&lt;remarks/&gt;
  &lt;System.Xml.Serialization.XmlElementAttribute("PreviousNames")&gt; _
  Public Property PreviousNames() As PreviousNameType()
      Get
          Return Me.previousNamesField
      End Get
      Set(value As PreviousNameType())
          Me.previousNamesField = value
      End Set
  End Property
  
  Public Function Clone() As Object Implements System.ICloneable.Clone
  
      Return New DirectorPersonType With {.CountryOfResidence = CountryOfResidence, .DOB = DOB, .Forename = Forename, .Nationality = Nationality, .Occupation = Occupation, .OtherForenames = OtherForenames, .PreviousNames = PreviousNames, .ServiceAddress = ServiceAddress, .Surname = Surname, .Title = Title}
  
  End Function
  
  End Class
  </code></pre>
  
  <p>DirectorReturnType:</p>
  
  <pre><code>Partial Public Class DirectorReturnType
  
  Implements ICloneable
  
  Private itemField As Object
  
  '''&lt;remarks/&gt;
  &lt;System.Xml.Serialization.XmlElementAttribute("Corporate",
  GetType(CorporateOfficerType)), _
  System.Xml.Serialization.XmlElementAttribute("Person", 
  GetType(DirectorPersonType))&gt; _
  Public Property Item() As Object
      Get
          Return Me.itemField
      End Get
      Set(value As Object)
          Me.itemField = Value
      End Set
  End Property
  
  Public Function Clone() As Object Implements System.ICloneable.Clone
  
      Return New DirectorReturnType With {.Item = Item}
  
  End Function
  </code></pre>