什么是 [Serializable],我应该什么时候使用它? [英] What is [Serializable] and when should I use it?

查看:37
本文介绍了什么是 [Serializable],我应该什么时候使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有些类使用 [Serializable] 属性.

I found out that some classes use the [Serializable] attribute.

  • 是什么?
  • 我应该什么时候使用它?
  • 我将获得哪些福利?

推荐答案

是什么?

当您在 .Net 框架应用程序中创建对象时,您无需考虑数据如何存储在内存中.因为 .Net Framework 会为您处理这些.但是,如果要将对象的内容存储到文件、将对象发送到另一个进程或通过网络传输它,则必须考虑如何表示对象,因为您需要转换为不同的格式.这种转换称为序列化.

What is it?

When you create an object in a .Net framework application, you don't need to think about how the data is stored in memory. Because the .Net Framework takes care of that for you. However, if you want to store the contents of an object to a file, send an object to another process or transmit it across the network, you do have to think about how the object is represented because you will need to convert to a different format. This conversion is called SERIALIZATION.

序列化允许开发人员保存对象的状态并根据需要重新创建它,提供对象的存储以及数据交换.通过序列化,开发人员可以执行一些操作,例如通过 Web 服务将对象发送到远程应用程序、将对象从一个域传递到另一个域、将对象作为 XML 字符串通过防火墙传递,或者维护安全性或特定于用户的跨应用程序的信息.

Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.

SerializableAttribute 应用于一个类型,指示可以序列化此类型的实例.应用 SerializableAttribute 即使类还实现了 ISerializable 控制序列化过程的接口.

Apply SerializableAttribute to a type to indicate that instances of this type can be serialized. Apply the SerializableAttribute even if the class also implements the ISerializable interface to control the serialization process.

标记的类型中的所有公共和私有字段SerializableAttribute 默认是序列化的,除非类型实现了 ISerializable 接口来覆盖序列化过程.默认序列化过程不包括用 NonSerializedAttribute.如果可序列化类型的字段包含指针、句柄或其他特定于特定环境的数据结构,并且无法在不同环境中有意义地重构,那么您可能需要应用 NonSerializedAttribute 到该字段.

All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process. The default serialization process excludes fields that are marked with NonSerializedAttribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply NonSerializedAttribute to that field.

有关详细信息,请参阅 MSDN.

See MSDN for more details.

编辑 1

不将某些内容标记为可序列化的任何理由

Any reason to not mark something as serializable

在传输或保存数据时,您只需发送或保存所需的数据.因此,传输延迟和存储问题会更少.因此,您可以在序列化时选择退出不必要的数据块.

When transferring or saving data, you need to send or save only the required data. So there will be less transfer delays and storage issues. So you can opt out unnecessary chunk of data when serializing.

这篇关于什么是 [Serializable],我应该什么时候使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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