如何比较两个.NET对象图之间的差异? [英] How to compare two .NET object graphs for differences?
问题描述
在我们的客户端/服务器应用程序中,我们一直在使用BinaryFormatter进行序列化过程.出于性能原因,我们正尝试迁移到protobuf-net( http://code.google.com/p/protobuf-net/).
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.
现在,我正在寻找一种方法来确保使用protobuf进行序列化和反序列化的数据与通常由BinaryFormatter处理的数据完全相同.
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.
一点一点的比较很简单:我使用BinaryFormatter序列化到文件.使用BinaryFormatter再次反序列化此文件.然后,我使用ProtoBuf序列化到一个文件中.从该文件中使用ProtoBuf反序列化.使用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.
但是,这两个文件不是100%相等.所以现在我需要找到一种方法来追踪差异.
However, those two files are not 100% equal. So now I need to find a way to track the differences down.
那里是否存在一些工具可以可视化BinaryFormatter序列化的数据?还是您知道其他一些帮助者,他们会进行深入比较并告诉我区别在哪里?
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?
使用XMLSerializer并比较两个XML文件是不可能的,因为BinaryFormatter能够比XMLSerializer序列化更多数据,甚至无需显式标记字段.
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.
谢谢你,TH
推荐答案
如何在启用对象跟踪的情况下使用 DataContractSerializer
(构造函数中的 preserveObjectReferences
).该应该允许您将它们序列化为xml(至少是某种类型),在这里您可以比较差异.
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.
这篇关于如何比较两个.NET对象图之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!