使用 BinaryFormatter 序列化不同但相等的对象图是否会产生相同的二进制表示形式?

发布于 2024-11-04 00:12:40 字数 188 浏览 3 评论 0原文

我正在研究基于对象模型的序列化对对象模型进行哈希处理。

如果我使用 .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 技术交流群。

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

发布评论

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

评论(1

烈酒灼喉 2024-11-11 00:12:40

我不知道这是有保证的,而且由于缺乏书面保证,我个人不会依赖它。特别是:

  • 任何实现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:

  • anything that implements ISerializable (i.e. custom serialization) can do whatever it wants
  • anything involving something like a dictionary may be unreliable - simply because there is no defined ordering inside a dictionary, and it may depend on the spare space in the original vs reconstituted, etc
  • there may be subtle compatible but not identical changes between platforms, revisions etc

However, 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 and BinaryFormatter 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.

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