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

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

问题描述

我发现了一些类使用 [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.

应用<一个href=\"http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx\"><$c$c>SerializableAttribute属性类型以指示此类型的实例可以被序列化。应用<一个href=\"http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx\"><$c$c>SerializableAttribute属性,即使该类还实现了<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx\"><$c$c>ISerializable接口来控制序列化进程。

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.

在一个类型的所有公共和私有字段由<一个标记href=\"http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx\"><$c$c>SerializableAttribute在默认情况下被序列化,除非该类型实现<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx\"><$c$c>ISerializable接口覆盖序列化进程。默认的序列化进程不包括标有<一个领域href=\"http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx\"><$c$c>NonSerializedAttribute属性。如果一个序列类型的字段包含一个指向,把手,或一些其它的数据结构,它是专用于一个特定的环境,并且不能在不同的环境中进行有意义的重构,则可能要应用的<一href=\"http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx\"><$c$c>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 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 了解更多详情。

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

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