开发者

VB.NET - Custom Serialization and Circular References, is there a clean solution?

开发者 https://www.devze.com 2023-01-09 19:04 出处:网络
I\'m implementing some custom serialization (to byte array), and have run into a problem handling circular references.

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?


You can use approach similar to standard BinaryFormatter from BCL: utilize ObjectIDGenerator and store reference id instead of object in case of circular references

0

精彩评论

暂无评论...
验证码 换一张
取 消