什么是ISerializable接口的地步? [英] What is the point of the ISerializable interface?

查看:242
本文介绍了什么是ISerializable接口的地步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好像我可以序列不具有该接口的类,所以我在它的目的还不清楚。

It seems like I can serialize classes that don't have that interface, so I am unclear on its purpose.

推荐答案

ISerializable的用于提供自定义的二进制序列化,通常的BinaryFormatter (或许为远程处理目的)。没有它,它使用的字段,它可以是:

ISerializable is used to provide custom binary serialization, usually for BinaryFormatter (and perhaps for remoting purposes). Without it, it uses the fields, which can be:


  • 低效;如果有仅用于在运行时效率域,但是可以为序列被移除(例如,字典可能看起来不同序列时)

  • 低效;因为即使是需要的领域,它需要包含很多附加的元数据

  • 无效;如果有字段不能序列化(如事件代表,虽然他们可以被标记 [非序列化]

  • 变脆;您的序列现在绑定到的字段的名字 - 但场,目的是要实现的细节;又见混淆,系列化,自动实现的属性

  • inefficient; if there are fields that are only used for efficiency at runtime, but can be removed for serialization (for example, a dictionary may look different when serialized)
  • inefficient; as even for fields that are needed it needs to include a lot of additional metadata
  • invalid; if there are fields that cannot be serialized (such as event delegates, although they can be marked [NonSerialized])
  • brittle; your serialization is now bound to the field names - but fields are meant to be an implementation detail; see also Obfuscation, serialization and automatically implemented properties

通过实施 ISerializable的您可以提供自己的二进制序列化机制。需要注意的是等价的xml的,这是的IXmlSerializable 13759 的XmlSerializer 等。

By implementing ISerializable you can provide your own binary serialization mechanism. Note that the xml equivalent of this is IXmlSerializable, as used by XmlSerializer etc.

有关DTO的目的,的BinaryFormatter 应避免 - (通过的XmlSerializer 的DataContractSerializer )或JSON都不错,因为是跨平台的格式,如协议缓冲区。

For DTO purposes, BinaryFormatter should be avoided - things like xml (via XmlSerializer or DataContractSerializer) or json are good, as are cross-platform formats like protocol buffers.

有关完整性,protobuf网不包括 ISerializable的钩(允许您使用便携式二进制格式,而无需编写大量的code),但的BinaryFormatter 就不会在这里你的首选呢。

For completeness, protobuf-net does include hooks for ISerializable (allowing you to use a portable binary format without writing lots of code), but BinaryFormatter wouldn't be your first choice here anyway.

这篇关于什么是ISerializable接口的地步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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