且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该在什么时候使用[Serializable接口]在C#中?

更新时间:2023-02-16 22:46:21

在创建.NET Framework应用程序的对象,你不需要考虑如何将数据存储在内存中。由于Net框架需要照顾你们。但是,如果要存储一个对象到一个文件中的内容,发送对象到另一个进程或通过网络发送它,你必须思考如何将对象重新presented,因为你将需要转换它为不同的格式。这种转换称为SERIALIZATION

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 .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 it 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$c$c>属性到类型以指示此类型的实例可以被序列化。应用SerializableAttribute$c$c>属性,即使该类也实现了ISerializable$c$c>接口来控制序列化进程。

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

被标记的SerializableAttribute$c$c>在默认情况下被序列化,除非该类型实现了ISerializable$c$c>接口重写序列化进程。默认的序列化过程中排除了标有NonSerializedAttribute$c$c>属性。如果一个序列类型的字段包含一个指向,把手,或一些其它的数据结构,它是专用于一个特定的环境,并且不能有意义地重建在不同的环境,则可能要应用的NonSerializedAttribute$c$c>归因于该领域。

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 the NonSerializedAttribute attribute. 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 the NonSerializedAttribute attribute to that field.

请参阅 MSDN 了解更多详细信息

See MSDN for more details