Reference integrity in BinaryFormatter

99封情书 提交于 2019-12-12 09:36:58

问题


The existence of AsReference option in Protobuf-net and the word that BinaryFormatter is a "graph serializer" lead me to assume that BinaryFormatter does not maintain references and that it makes a copy of every object.

But I did some tests and found out that all references in a single BinaryFormatter Serialize() or Deserialize() call are maintained even for recursive referencing.

Can I confirm that BinaryFormatter does indeed maintain references? How is this different from Protobuf-net? Seems like I understand "graph serialization" incorrectly? What else should I look out for?

Thanks in advance.


回答1:


tl;dr; version - BinaryFormatter always preserves references.

The fact that BinaryFormatter is a "graph serializer" is synonymous with "it preserves references", since that is required to accurately reproduce a graph. The alternative is a tree serializer (which is most of them; XmlSerializer, DataContractSerializer (without a particular option enabled), JavascriptSerializer and protobuf-net without AsReference are all tree-based). Tree serializers do not generally preserve references (unless they work some tricks, i.e. what DataContractSerializer does if you enable full-graph mode, or protobuf-net does with AsReference). Tree serializers (without voodoo enabled) tend to explode in a mess with a recursive model, which makes them easy to spot.



来源:https://stackoverflow.com/questions/7479114/reference-integrity-in-binaryformatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!