我应该在什么时候使用[Serializable接口]在C#中? [英] When should i use [Serializable] in C#?

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

问题描述

我发现了一些类使用[Serializable]属性。

I found some that classes use this [Serializable] attribute.

  • 这是什么?
  • 在什么时候应该使用它?
  • 我会得到什么样的好处?

请用简单的例子解释了我,因为我只是一个初学者。

Please explain me with simple examples since I'm just a beginner.

推荐答案

在创建.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.

应用<一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 了解更多详细信息

See MSDN for more details

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

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