问题
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