子级引用其父级的复杂类型的反序列化问题

发布于 2024-10-20 16:21:28 字数 1801 浏览 3 评论 0原文

我和孩子们一起上课。所有孩子都必须有对我的根对象的引用。一切都很顺利,直到我反序列化我的对象。发生反序列化时,反序列化会对我的子对象执行 New(),因此即使在调用 SetParent 之前,该子对象已被新对象替换,并且没有调用 SetParent。因此,在反序列化之后,我的子对象都不知道他的父对象。对象 Root 被很多应用程序使用,我不希望所有这些应用程序都调用 SetParent。

我正在寻找 AfterDeserialization 事件,但没有找到任何事件。我已经通过反射进行了查看,但没有找到找到父对象的方法。我已经看到我可以实现 ISerialized,但发现管理所有反序列化过程有点繁重(我在这个对象中有大约 170 个属性)。

我可以实现 ISerialized 并调用执行所有操作的基本方法,然后只调用我的 SetParent 函数吗?或者有没有一种方法可以通过反射来找到我在研究中未找到的对象实例的父级?或者有人会有其他建议吗?

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

感谢您的帮助! :o)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

氛圍 2024-10-27 16:21:28

是否可以在父属性设置器中设置父项?

如果您的父级也进行序列化,则反序列化将调用设置器。

前任:

   Public Class Root
       ...
       Public Property a() As Child1  
        Get       
            Return _a     
        End Get
        Set(ByVal value As Child1)      
           _a = value    
           _a.SetParent(Me)
        End Set   
      End Property 
      ...
   End Class

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:

   Public Class Root
       ...
       Public Property a() As Child1  
        Get       
            Return _a     
        End Get
        Set(ByVal value As Child1)      
           _a = value    
           _a.SetParent(Me)
        End Set   
      End Property 
      ...
   End Class
猫烠⑼条掵仅有一顆心 2024-10-27 16:21:28

您无法使用 XML 序列化来执行此操作,除非您实现 IXmlSerialized 并自行完成所有工作。

根据您使用的 .NET 版本,您可以尝试使用 'NetDataContractSerializer',因为它可以处理对同一对象的多个引用。

You can't do this using XML Serialization, unless you implement IXmlSerializable and do all the work yourself.

Depending on which version of .NET you are using, you might try using the 'NetDataContractSerializer', since it can handle multiple references to the same object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文