标志着一类为Serializable的缺点 [英] Drawbacks of marking a class as Serializable

查看:421
本文介绍了标志着一类为Serializable的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是标志着一类的缺点序列化?

我需要保存在一个数据库我的asp.net会话,它要求在会话中的对象序列化。

请感。

不过,事实证明,我必须做的是装饰与 [Serializable接口] 属性,它的类和它的工作,所以这意味着.NET已经拥有了基本的基础设施,使类序列化。那么,为什么不能把它只是做了默认?

什么是将其标记为这样的需求?


解决方案

  

那么,为什么不能只是在默认情况下做到这一点?


自动序列化/反序列化可能不足以为对象。例如,对象可能包含持有的本地文件的名称的字段,指向存储器,一个索引到一个共享阵列等虽然系统可以典型地串行化这些原始值不麻烦,反序列化很容易导致事是不使用的。在一般情况下,这是不可能的,系统算出这个自身。通过要求您标记与序列化类,即表示您已经采取了以下因素考虑在内。

What are the drawbacks of marking a class as serializable?

I need to save my asp.net session in a db and it requires that the objects in the session are serializable.

Make sense.

But turns out that all I had to do was decorate that class with the [Serializable] attribute and it worked, so that means .NET already has the underlying infrastructure to make classes serializable. So why can't it just do it by default?

What's the need to mark it as such?

解决方案

So why can't it just do it by default?

Automatic serialization/deserialization might not suffice for the object. For example, the object might contain a field that holds the name of a local file, a pointer to memory, an index into a shared array, etc. While the system could typically serialize these raw values without trouble, deserialization could easily result in something that is not usable. In general, it is impossible for the system to figure this out on its own. By requiring you to mark the class with Serializable, you indicate that you have taken these considerations into account.

这篇关于标志着一类为Serializable的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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