VB.NET - 自定义序列化和循环引用,有干净的解决方案吗?

发布于 2024-09-12 05:17:12 字数 472 浏览 10 评论 0原文

我正在实现一些自定义序列化(到字节数组),并且遇到了处理循环引用的问题。

示例:

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 技术交流群。

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

发布评论

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

评论(1

不奢求什么 2024-09-19 05:17:12

您可以使用类似于 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

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