1
情況是這樣的:其中母體具有參照子反之亦然VB.Net克隆層級 - >循環引用
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
實際的對象是更復雜的,具有若干等級。 我不知道如何解決這個問題,因爲目前,只要您在父級A
類別上致電Clone
,您將最終得到循環引用。
我該如何避免這種情況?我應該創建自己的Clone
函數並傳遞參數嗎?
謝謝史蒂文。雖然我以另一種方式解決了這個問題(對象從根本上改變了),但您的解決方案將是一個好方法。 – Recipe 2014-11-05 09:01:40