.NET二进制序列化对象与其他对象的引用。 。 。怎么了? [英] .NET Binary Serialize object with references to other objects . . . what happens?

查看:183
本文介绍了.NET二进制序列化对象与其他对象的引用。 。 。怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个对象实例的一个引用其他对象(例如实例B和C),和你的二进制序列化到一个文件中,会发生什么?你现在已经序列化的数据,包括A,B和C?

If you have an object instance A that references other objects (for example instances B and C), and you binary serialize A to a file, what happens? Do you now have serialized data that includes A, B and C?

它是如何工作是什么呢?我将要得到,如果我反序列化的数据? A,B和C ??

How does it work exactly? What will I get if I deserialize the data? A, B, and C??

(随时包括内部运作​​的解释也一样)。

(Feel free to include internal workings explanations as well).

推荐答案

所有引用到其他对象将被序列为好。如果您反序列化的数据,你会最终有一个完整的,工作集的数据,包括对象A,B和C这可能是二进制序列化的主要好处,而不是XML序列化。

All of the references to other objects will be serialized as well. If you deserialize the data, you will end up with a complete, working set of its data, including objects A, B, and C. That's probably the primary benefit of binary serialization, as opposed to XML serialization.

如果任何其他类的对象包含的引用,都没有标明<一个href="http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx"><$c$c>[Serializable]属性,你会得到一个<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.serializationexception.aspx"><$c$c>SerializationException在运行时(图像,其中被无耻地从网络上窃取;运行时错误甚至不看像这样继续下去了在VS的当前版本):

If any of the other classes your object holds a reference to are not marked with the [Serializable] attribute, you'll get a SerializationException at run-time (the image of which was shamelessly stolen from the web; run-time errors don't even look like this anymore in the current versions of VS):

&NBSP;&NBSP;&NBSP;&NBSP;

    

另外重要的是,我真的不知道什么是内在的东西你希望理解。序列化使用反射穿行对象的公共和私人领域,将它们转换为流字节,这最终是写出到的数据流。期间反序列化,逆发生:字节流从数据流中,这是用于合成对象完全相同的复制,以及类型信息读入。所有对象的字段有,他们之前持有相同的价值观;构造函数是的没有的所谓当一个对象被反序列化。想一想最简单的办法是,你只是采取的快照就地的对象,就可以恢复到任意原来的状态。

Further than that, I'm not really sure what "internal things" you were hoping to understand. Serialization uses reflection to walk through the public and private fields of objects, converting them to a stream of bytes, which are ultimately written out to a data stream. During deserialization, the inverse happens: a stream of bytes is read in from the data stream, which is used to synthesize an exact replicate of the object, along with type information. All of the fields in the object have the same values that they held before; the constructor is not called when an object is deserialized. The easiest way to think about it is that you're simply taking a snapshot-in-place of the object, that you can restore to its original state at will.

这是负责实际序列化和反序列化被称为格式化的类(它总是从<一个继承href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iformatter.aspx"><$c$c>IFormatter接口)。它的任务是生成一个对象图,这是一个包含反序列化作为其根正被序列化对象/广义树。如上所述,格式化器使用反射来遍历这个对象图,序列/反序列化包含由该对象的所有对象引用。格式化也是足够的智能知道不要超过一次序列图中的任何对象。如果两个对象引用实际却指向同一个对象,这将检测到该对象将只一次序列化。这和其他逻辑$ P进入一个无限循环$ pvents。

The class that is responsible for the actual serialization and deserialization is called a formatter (it always inherits from the IFormatter interface). It's job is to generate an "object graph", which is a generalized tree containing the object that is being serialized/deserialized as its root. As mentioned above, the formatter uses reflection to walk through this object graph, serializing/deserializing all object references contained by that object. The formatter is also intelligent enough to know not to serialize any object in the graph more than once. If two object references actually point to the same object, this will be detected and that object will only be serialized once. This and other logic prevents entering an infinite loop.

当然,很容易有此过程如何工作的好大致的了解。它的的难以实际写入code实现它自己。幸运的是,这已经为你做。 .NET框架的观点之一是,所有这些复杂的序列化的逻辑是内置的,让你免于担心它。我不主张明白这一切我自己,你当然不需要要么利用它提供的功能,充分利用。多年手工编写所有code是终于结束了。你应该感到高兴,而不是担心实施细节。 : - )

Of course, it's easy to have a good general understanding of how this process works. It's much harder to actually write the code that implements it yourself. Fortunately, that's already been done for you. Part of the point of the .NET Framework is that all this complicated serialization logic is built in, leaving you free from worrying about it. I don't claim to understand all of it myself, and you certainly don't need to either to take full advantage of the functionality it offers. Years of writing all that code by hand are finally over. You should be rejoicing, rather than worrying about implementation details. :-)

这篇关于.NET二进制序列化对象与其他对象的引用。 。 。怎么了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆