什么架构使用system.runtime.serialization.ISerialized?
我确实有一些类实现了自己的序列化/反序列化,但我在执行所有这些操作时没有实现 system.runtime.serialization.ISerialized。所以我的问题是使用 system.runtime.serialization.ISerialized 到底有什么好处?
在什么情况下我们需要实现system.runtime.serialization.ISerialized?
I do have some classes that implement their own serialization/deserialization but I'm doing all of these without implementing system.runtime.serialization.ISerializable. so my question is what exactly is the benefit of using system.runtime.serialization.ISerializable ?
in what example situation will we need to implement system.runtime.serialization.ISerializable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的序列化不与标准 .net 序列化机制集成。您不能将其与
BinaryFormatter
一起使用。ISerialized
使得 a) 控制序列化 b) 将其集成到二进制序列化的标准机制中成为可能。另外,您决定编写自己的序列化也很奇怪。编写高效的生产质量序列化是相当昂贵的,特别是当.net中有可供使用的二进制和XML序列化以及一些很酷的第三方序列化(例如Protobuf和Thrift)时。
实际上,ISerialized 页面 上给出了很好的示例MSDN。
当您想要连接二进制序列化时,一个现实生活中的例子是它在序列化
DateTime
时丢失时区。 (至少在.net 2.0中)You serialization doesn't integrate with standard .net serialization mechanisms. You can't use it with
BinaryFormatter
.ISerializable
makes possible to a) control serialization b) integrate it into standard mechanism of binary serialization.Also it is very strange that you decided to write your own serialization. This is rather expensive to write efficient production quality serialization, especially when there are binary and XML serializations ready to use in .net and couple of cool 3rd party ones, like Protobuf and Thrift.
Great example is actually given at ISerializable page on MSDN.
One real life example when you would want to hook into binary serialization is that it looses timezones when serializing
DateTime
. (At least in .net 2.0)当您需要保存比仅序列化字段更复杂的数据时,
BinaryFormatter
会使用ISerialized
- 例如,您想要以不同的方式处理数据(可能是压缩的 - 例如,字典在内存中相当大,但可以只是线路上的键/值列表)。同样的“链表” - 您将再次希望在一个对象(列表)的序列化中迭代该对象,而不仅仅是序列化第一个节点,并让该节点序列化自身和下一个节点, ETC。ISerializable
is used byBinaryFormatter
when you need to persist data that is a bit more complex than just serializing the fields - for example, you want to process the data differently (maybe condensed - for example, a dictionary is pretty expansive in memory, but can just be a key/value list on the wire). Likewise a "linked list" - you would again want to iterate that in the serialization of one object (the list), rather than just serialize the first node, and have that node serialize itself and the next node, etc.