I have a class with childrens. All childs must have a reference to my root object. Everything is going fine until I deserialize my object. When deserialization occurs, Deserialization do a New() on my child objects so even if before a SetParent has been called, that child object has been replace by a new one and no SetParent has been called. So after Deserialization, none of my child objects know his parent. The object Root is use by a lot of applications and i don’t want all those applications to call the SetParent.
I’ve look for a event AfterDeserialization but haven’t found any. I’ve look through reflection and haven’t found a way to find the parent object. I’ve seen I could Implements ISerializable but find it a bit heavy to manage all the deserialization process (I have about 170 properties in this object).
Can I Implements ISerializable and call a base method that do all the things and after, just call my SetParent function? Or is there a way with reflection to find the parent of an instance of an object that I haven’t found in my research? Or anyone would have any other suggestions?
Public Class Root
Private _a As Child1
Private _b As Child2
Public Property a() As Child1
Get
Return _a
End Get
Set(ByVal value As Child1)
_a = value
End Set
End Property
Public Property b() As Child2
Get
Return _b
End Get
Set(ByVal value As Child2)
_b = value
End Set
End Property
Public Sub New()
a = New Child1
b = New Child2
SetParent()
End Sub
Friend Sub SetParent()
a.SetParent(Me)
b.SetParent(Me)
End Sub
End Class
Public Class Child1
Private _parent As Root
Friend Sub SetParent(ByRef parent As Root)
_parent = parent
End Sub
End Class
Public Class Child2
Private _parent As Root
Private _a As New Child3
Public Property a() As Child3
Get
Return _a
End Get
Set(ByVal value As Child3)
_a = value
End Set
End Property
Friend Sub SetParent(ByRef parent As Root)
a = New Child3
_parent = parent
a.SetParent(parent)
End Sub
End Class
Public Class Child3
Private _parent As Root
Friend Sub SetParent(ByRef parent As Root)
_parent = parent
End Sub
End Class
Thanks for your help! :o)
Is it possible to set the parent in the parent property setter?
If you parent is serialize too, the deserialization will call the setter.
ex: