问题描述
我发现了一些类使用 [Serializable接口]
属性。
I found out that some classes use the [Serializable]
attribute.
- 这是什么?
- 我什么时候应该使用它?
- 我会得到什么样的好处?
推荐答案
当您创建.NET Framework应用程序的对象,你不需要考虑数据是如何存储在内存中。由于Net框架需要照顾你们。但是,如果要存储一个对象到一个文件中的内容,发送对象到另一个进程或通过网络发送它,你必须思考的对象是如何重新presented,因为你将需要转换它为不同的格式。这种转换被称为序列化。
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.
应用<$c$c>SerializableAttribute$c$c>属性类型以指示此类型的实例可以被序列化。应用<$c$c>SerializableAttribute$c$c>属性,即使该类还实现了<$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.
在一个类型的所有公共和私有字段由<$c$c>SerializableAttribute$c$c>在默认情况下被序列化,除非该类型实现<$c$c>ISerializable$c$c>接口覆盖序列化进程。默认的序列化进程不包括标有<$c$c>NonSerializedAttribute$c$c>属性。如果一个序列类型的字段包含一个指向,把手,或一些其它的数据结构,它是专用于一个特定的环境,并且不能在不同的环境中进行有意义的重构,则可能要应用的<$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.
请参阅了解更多详情。
这篇关于什么是[Serializable接口]什么时候应该使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!