This is the situation:
Class A
Implements ICloneable
Public Property Children As List(Of Child)
Public Function Clone() As Object Implements ICloneable.Clone
Return New A With {
.Children = Children.Select(Function(c) DirectCast(c.Clone(), Child)).ToList()
}
End Function
End Class
Class Child
Implements ICloneable
Public Property Parent As A
Public Function Clone() As Object Implements ICloneable.Clone
Return New Child With {
.Parent = DirectCast(Parent.Clone(), A)
}
End Function
End Class
The actual object is more complex, having several levels.
I'm not sure how to solve this because, at the moment, whenever you call Clone
on the parent A
class, you will end up with a circular reference.
How can I avoid this situation? Should I create my own Clone
function and pass along a parameter?