VB.NET - 自定义序列化和循环引用,有干净的解决方案吗?
我正在实现一些自定义序列化(到字节数组),并且遇到了处理循环引用的问题。
示例:
Class A
public MyBs as new List(of B)
End class
Class B
public MyParent as A
public MiscInt1 as integer
public MiscInt2 as integer
End Class
当序列化A时,我必须序列化B的每个实例。
但是,我在序列化B时遇到问题。
如何记录B的父级而不导致无限循环?
一个想法:
如果我知道 B 的实例只能通过 A 实例的序列化来序列化,那么我可以处理 A 实例中 MyParent 引用的设置,甚至不将该信息记录在字节缓冲区内对于 B 的例子,
这感觉不太正确,但它可能是最好的解决方案。
有没有更干净的方法来处理这种情况?
I'm implementing some custom serialization (to byte array), and have run into a problem handling circular references.
Example:
Class A
public MyBs as new List(of B)
End class
Class B
public MyParent as A
public MiscInt1 as integer
public MiscInt2 as integer
End Class
When serializing A, I must serialize each instance of B.
However, I have a problem when serializing B.
How do I record the parent of B without causing an infinite loop?
An idea:
If I know that an instance of B will only ever be serialized through the serialization of an instance of A, then I can handle the setup of the MyParent reference from the instance of A and not even record that information inside the byte buffer for the instance of B
This doesn't feel quite right, but it might be the best solution.
Is there a cleaner way of handling this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用类似于 BCL 中的标准 BinaryFormatter 的方法:利用 ObjectIDGenerator 并在循环引用的情况下存储引用 id 而不是对象
You can use approach similar to standard BinaryFormatter from BCL: utilize ObjectIDGenerator and store reference id instead of object in case of circular references