继承树上的反序列化顺序 [英] Deserialization order over the inheritance tree
问题描述
我有一个标记为 Serializable
的基类,派生类也标记为 Serializable
。我想在反序列化过程中在基类中做一些事情,因此声明了一个标记为 OnDeserializing
的方法,但重要的是,此方法将在任何派生类的 OnDeserializing
方法。
派生类也可能是其他人编写的。
我可以相信在任何序列化方法之前都会调用基类的方法吗?
我将使用 SoapFormatter
。
I have a base class marked Serializable
, and derived classes marked Serializable
too. I want to do something in the base class during deserialization, and therefore declared a method marked OnDeserializing
, but it's important that this method will execute before any derived class's OnDeserializing
methods.
Derived classes might be written by others too.
Can i rely that the base class's method will be called prior to any serialization method in derived classes?
I gonna use SoapFormatter
.
推荐答案
我要走了一条路,说它将可能首先找到派生的方法,而不是基础的方法。基于我所看到的将反射用于其他目的的方法。我建议您可能要从子级显式调用基本行为,而不要依赖标记有该属性的方法的任何顺序。如果较低层的对象是直接可实例化的,而不是抽象的,因此也可以使用OnDeserializing标记的方法,则可能需要设置标志以指示该层是否已被处理。
I'm going to go out on a limb and say that it will probably find the derived methods first rather than the base methods based on what I've seen using reflection for other purposes. I suggest that you might want to explicitly call the base behavior from the child rather than rely on any ordering of the methods marked with the attribute. If lower level objects are directly instantiable, rather than abstract, and thus may also have an OnDeserializing-marked method, you might need to set flags to indicate whether that level has already been processed.
这篇关于继承树上的反序列化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!