在抽象类序列化上强制序列化 [英] Enforce serialization on abstract class serialization

查看:192
本文介绍了在抽象类序列化上强制序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个抽象类说A.该类将被许多其他开发人员继承.
我希望该类可序列化,即从我的基类继承的所有内容也应可序列化.
我如何保证他们将[Serializable]添加到其实现中?
是否可以强制执行此操作?我的抽象类中的[Serializable]属性不是继承的,但这就是我所追求的功能.

基本上,我想保护我的
[Serializable]
B类
{
列表< A> ;;
}
在序列化B时崩溃,因为有人忘记将[Serializable]属性添加到其A继承的实现中.

/Jongas

Hi,
I have an abstract class say A. This class will be inherited by many other developers.
I want this class to be serializable, i.e. all inheriting from my base class should also be serializable.
How can I garantee that they will add [Serializable] to their implementations?
Is there a way of enforcing this? The [Serializable] attribute in my abstract class is not inherited but that would be the functionallity I'm after.

Basically I'd like to guard my
[Serializable]
Class B
{
List<A>; 
}
to crash when serializing B because someone forgot to add the [Serializable] attribute to their A inherited implementation.

/Jongas

推荐答案

您在构造函数中唯一可以做的就是检查最终类是否具有Seri​​alizable属性.如果不是,请抛出异常.

但是,请花一些时间来看看此
The only thing you can do in your constructor is to check whether the final class has the Serializable attribute. If not, throw an exception.

However, spare yourself some time and take a look at this Data Object Base.


这篇关于在抽象类序列化上强制序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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