How to compare two .NET object graphs for differences?

无人久伴 提交于 2021-02-18 21:09:47

问题


In our Client/Server Application we've been using BinaryFormatter for the serialization process. For performance reasons we are trying to migrate to protobuf-net ( http://code.google.com/p/protobuf-net/ ).

Our software transmits huge graphs with cycles between Client and Server.

Now I am looking for a way to make sure that the data which was serialized and deserialized using protobuf is exactly the same as the one which was usually processed by BinaryFormatter.

A bit by bit comparison is simple: I serialize using BinaryFormatter to a file. Deserialize this file again using BinaryFormatter. Then I serialize using ProtoBuf into a file. Deserialize using ProtoBuf from that file. Serialize again using BinaryFormatter into a file.

Then i can simply compare that file with the original file.

However, those two files are not 100% equal. So now I need to find a way to track the differences down.

Is there maybe some tool out there which visualizes data that was serialized by BinaryFormatter? Or do you know some other helper which does a deep comparison and tells me where the differences are?

Using XMLSerializer and comparing two XML files is not possible as BinaryFormatter is able to serialize way more data than the XMLSerializer - even without marking fields explicitly.

Thank you, TH


回答1:


How about using DataContractSerializer with object-tracking enabled (preserveObjectReferences in the constructor). That should allow you to serialize them to xml (of a sort, at least), where you might be able to compare the differences.




回答2:


We had the same problem. We Json serialize the two objects (with formatting, indentation, new lines etc) and then use a simple text diff. It will tell you not only that they're different, but exactly what the difference is.



来源:https://stackoverflow.com/questions/6661259/how-to-compare-two-net-object-graphs-for-differences

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