Reputation:
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.
Upvotes: 3
Views: 243
Reputation: 1062550
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 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.
Upvotes: 1