ICloneable deepcopy объекта в.net
Я пытаюсь добавить метод глубокого копирования с использованием ICloneable к классам, которые были автоматически сгенерированы из xsd с использованием xsd.exe. Я могу заставить его работать на простом уровне, но как только объекты становятся вложенными, метод клона не работает.
Я почти уверен, что у меня неправильный метод clone в классе DirectorReturnType, но я не могу понять, как это исправить.
Кто-нибудь может предложить какую-либо помощь? Я приложил подводные лодки и классы ниже:
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
Это работает, как ожидается, и сообщения "test1", а затем "test2":
'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)
Если я затем добавлю дополнительный объект AROTx типа AnnualRetunOfficer, он выдаст сообщение "Test2", а затем "Test2".
'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)
DirectorPersonType:
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
'''<remarks/>
Public Property ServiceAddress() As ServiceAddressType
Get
Return Me.serviceAddressField
End Get
Set(value As ServiceAddressType)
Me.serviceAddressField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(DataType:="date")> _
Public Property DOB() As Date
Get
Return Me.dOBField
End Get
Set(value As Date)
Me.dOBField = value
End Set
End Property
'''<remarks/>
Public Property Nationality() As String
Get
Return Me.nationalityField
End Get
Set(value As String)
Me.nationalityField = value
End Set
End Property
'''<remarks/>
Public Property Occupation() As String
Get
Return Me.occupationField
End Get
Set(value As String)
Me.occupationField = value
End Set
End Property
'''<remarks/>
Public Property CountryOfResidence() As String
Get
Return Me.countryOfResidenceField
End Get
Set(value As String)
Me.countryOfResidenceField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("PreviousNames")> _
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
DirectorReturnType:
Partial Public Class DirectorReturnType
Implements ICloneable
Private itemField As Object
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Corporate",
GetType(CorporateOfficerType)), _
System.Xml.Serialization.XmlElementAttribute("Person",
GetType(DirectorPersonType))> _
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
1 ответ
Ваш Клон на самом деле не делает свежую копию нескольких переменных, он указывает на существующие значения, хранящиеся в вашем исходном классе.
А именно эти две переменные:
Private serviceAddressField As ServiceAddressType
Private previousNamesField() As PreviousNameType
Потребуется особое внимание в вашей функции клонирования.
Это аккуратно (возможно, не совсем стандартно), чтобы позвонить ME.MemberwiseClone
чтобы получить новую мелкую копию вашего экземпляра. Затем вы ДОЛЖНЫ иметь дело со своими нестандартными типами (массивами, классами) по отдельности и создавать свежие их копии.
Что-то вроде (пожалуйста, исправьте любые ошибки, но это общая идея)
Public Function Clone() As Object Implements ICloneable.Clone
Dim typClone As DirectorPersonType = Me.MemberwiseClone ' Shallow clone taken and new object created
Try
typClone.serviceAddressField = Me.serviceAddressField.Clone ' create a new copy with same value
typClone.previousNamesField.AddRange(Me.previousNamesField) ' create a new copy with the same values
' PreviousNamesField will need some work generating a new array
' let me know if you need more detail or a working example
' However, I have been corrected and will share that this method is
' definitely superior to AddRange, using LINQ to
' create a new list with a copy of the items
typClone.ListName = Me.ListName.Select(Function(x) x.Clone()).Cast(Of ClassName).ToList
Catch ex As Exception
' catch errors and handle here!
End Try
Return typClone
End Function