什么是[Serializable接口]什么时候应该使用它?

编程入门 行业动态 更新时间:2024-10-23 10:33:32
本文介绍了什么是[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.

应用SerializableAttribute属性类型以指示此类型的实例可以被序列化。应用SerializableAttribute属性,即使该类还实现了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.

在一个类型的所有公共和私有字段由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 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接口]什么时候应该使用它?

本文发布于:2023-06-07 15:23:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568562.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   接口   使用它   Serializable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!