为什么必须用属性 [serializable] 标记一个类? [英] Why do you have to mark a class with the attribute [serializable]?

查看:31
本文介绍了为什么必须用属性 [serializable] 标记一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以将任何文档转换为字节数组并将其保存到磁盘,然后将文件重建为其原始形式(只要您有文件名的元数据等).

Seeing as you can convert any document to a byte array and save it to disk, and then rebuild the file to its original form (as long as you have meta data for its filename etc.).

为什么你必须用 [Serializable] 等标记一个类?这与元数据"类型信息是否相同,因此当您将对象转换为其类时,事情会正确映射?

Why do you have to mark a class with [Serializable] etc? Is that just the same idea, "meta data" type information so when you cast the object to its class things are mapped properly?

推荐答案

首先,你没有.

它只是一个 标记接口,它告诉序列化程序该类由以下项组成它可以序列化(可能是也可能不是),也就是可以使用默认的序列化.

It is simply a marker interface that tells the serializer that the class is composed of items that it can serialize (which may or may not be true) and that is can use the default serialization.

XMLSerializer 有额外的要求,即类的构造函数为零参数.

The XMLSerializer has the additional requirement to have a zero parameter constructor to the class.

还有其他使用序列化合同的序列化程序(例如 DataContractSerializer) - 与简单地将类标记为 Serializable 相比,它们可以让您更好地控制序列化.您还可以通过实现 ISerializable 来获得更多控制权 接口.

There are other serializers that use contracts for serialization (such as the DataContractSerializer) - they give you more control over serialization than simply marking a class as Serializable. You can also get more control by implementing the ISerializable interface.

这篇关于为什么必须用属性 [serializable] 标记一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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