Deserialization order over the inheritance tree

蓝咒 提交于 2019-12-10 17:52:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13664008/deserialization-order-over-the-inheritance-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!