为什么需要一个对象序列化属性进行序列化 [英] Why is Serializable Attribute required for an object to be serialized

查看:353
本文介绍了为什么需要一个对象序列化属性进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,SerializableAttribute没有提供编译时检查,因为这一切都在运行时完成。如果是这样的话,那么为什么它需要的类被标记为可序列化?

Based on my understanding, SerializableAttribute provides no compile time checks, as it's all done at runtime. If that's the case, then why is it required for classes to be marked as serializable?

无法序列化只是试图序列化一个对象,然后失败?这不正是它的权利吗?当一些被标记,它会尝试和失败。那岂不是如果你有标记的东西为不可序列化的,而不是串行化的更好吗?这样,你就不会有库没有标记物为可序列化?

Couldn't the serializer just try to serialize an object and then fail? Isn't that what it does right now? When something is marked, it tries and fails. Wouldn't it be better if you had to mark things as unserializable rather than serializable? That way you wouldn't have the problem of libraries not marking things as serializable?

推荐答案

据我所知,背后的想法 SerializableAttribute 是创建一个选入系统二进制序列化。

As I understand it, the idea behind the SerializableAttribute is to create an opt-in system for binary serialization.

请记住,不像XML序列化,它使用的公共属性,二进制序列化默认情况下抓住所有的私有字段。

Keep in mind that, unlike XML serialization, which uses public properties, binary serialization grabs all the private fields by default.

这不仅可能包括操作系统结构和私有数据是不应该被曝光,但反序列化可能导致腐败的状态可能会崩溃的应用程序(傻例如:a。手柄打开文件在不同的计算机)。

Not only this could include operating system structures and private data that is not supposed to be exposed, but deserializing it could result in corrupt state that can crash an application (silly example: a handle for a file open in a different computer).

这篇关于为什么需要一个对象序列化属性进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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