简体   繁体   中英

Is there a chance of saving the hashcode of an object during its binary serialization (binary)?

I want to be able to compare objects by the hashcode.

Per example, one is the object itself, and the other is serialized (binary) and then recovered version of the object.

How can I save the hash in the serialized (binary) object?

Why would you have to serialize the hash code? Instead you should provide a proper implementation of GetHashCode() and Equals() in your object that allows you to compare two objects based on their values - if two objects are equal their hash codes have to match. So once you have deserialized the object, you can use GetHashCode() on it and compare it with the other object. Note that the fact that two hash codes match is not enough to determine equality, they might still be different - you will have to call a proper implementation of Equals() to determine equality.

If you just want to compare custom fields within an object and a full comparison might be too expensive (ie a large binary array) it might make sense to generate an MD5 hash (ie with MD5CryptoServiceProvider.ComputeHash() ) on the field and store that within the object itself, it will then be serialized just like any other object property.

Be wary!

The default HashCode of a .Net object often changes between run-time instances of a program.

In other words, if your program serializes object A , complete with hashcode, to the disc, then the program terminates, and is later restarted, and de-serializes object A from disc, (or creates an identical object A at run-time), it will have a different hashcode than what was stored.

This is in part because the default hashcode comes from the Garbage Collectors information on an object. In a new program instance, the GC will have different information, and thus a different hashcode.

If you write your own GetHashCode , you can make a hashcode that is consistent across processes. But there is a pitfall here you need to be aware of.

Is there any information which you can use to tell which objects were serialized and deserialized from which originals? If so, then you can override GetHashCode() to calculate a hash code based on that information.

If not, you might be able to generate one synthetically by assigning a UUID to each newly-created object. Serialize that value along with the other data so the reconstructed objects have the same UUID. You can then simply override GetHashCode() to return that UUID's hash code. (That should do the job if what you're looking for is a sort of modified version of referential equality.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM