为什么必须用属性 [serializable] 标记一个类?

Why do you have to mark a class with the attribute [serializable]?(为什么必须用属性 [serializable] 标记一个类?)
本文介绍了为什么必须用属性 [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] 标记一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)