使用 BinaryFormatter 序列化不同但相等的对象图是否会产生相同的二进制表示形式?
我正在研究基于对象模型的序列化对对象模型进行哈希处理。
如果我使用 .NET BinaryFormatter 序列化对象图,对于所有涉及的对象都由相同值组成的另一个对象图,序列化表示形式是否保证逐字节完全相同?
直觉上,我是这么认为的,但我不确定对象/引用 ID 是否会以某种方式影响实际的序列化表示。
提前致谢。
I'm looking at hashing an object model, based on a serialization of it.
If I serialize an object graph using the .NET BinaryFormatter, is the serialized representation guaranteed to be the exact same, byte for byte, for another object graph where all the involved objects are composed of the same values?
Intuitively, I'd think so, but I'm unsure if object/reference IDs might somehow influence the actual serialized representation.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是有保证的,而且由于缺乏书面保证,我个人不会依赖它。特别是:
ISerialized
(即自定义序列化)的东西都可以做任何它想做的事情但是,如果您是谈论二进制等价,我推断您正在谈论
BinaryFormatter
数据的存储,以及稍后的后续比较。请允许我强调:IMO,BinaryFormatter
不适合存储;在共享平台和应用程序版本的运行系统之间传输数据是可以的,但是我见过很多次由于版本差异和 BinaryFormatter 导致人们陷入困境压倒。正是由于这些原因,我对 protobuf 产生了兴趣,并编写了自己的序列化器,该序列化器适合针对现有对象模型进行存储和重新水化,这些对象模型在临时。I do not know that is guaranteed, and with the lack of a documented guarantee I would not personally rely on it. In particular:
ISerializable
(i.e. custom serialization) can do whatever it wantsHowever, if you are talking about binary equivalence, I deduce you are talking about storage of
BinaryFormatter
data, and subsequent comparison at a later date. Allow me to stress: IMO,BinaryFormatter
is not suitable for storage; it is OK (ish) for transport of data between running systems that share a platform and application version, but the sheer number of times I've seen people stuck in a hole due to version differences andBinaryFormatter
is overwhelming. It is for these reasons that I got interested in protobuf, and wrote my own serializer that would be suitable for storage and rehydration against existing object models that make have "versioned" in the interim.