3

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.

RoadBump
  • 733
  • 7
  • 16
  • I'm using a IFormatter. Cannot use Serializer such as XmlSerializer for some reasons. – RoadBump Dec 01 '12 at 22:28
  • Another helpful question [here](http://stackoverflow.com/questions/4023644/can-ondeserializedattribute-be-used-instead-of-ideserializationcallback-interfac). From my tests the order is `Base.OnDeserializing`,`Derived.OnDeserializing`,`Base.OnDeserialized`,`Derived.OnDeserialized`, but it may depend in the exact structure of the object graph. – RoadBump Dec 18 '12 at 23:14

1 Answers1

2

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.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Forgotten to write, but derived classes might be created by others, so i have no control on it. Thank you for the important information, anyway. It's very helpful. – RoadBump Dec 01 '12 at 22:03
  • @RoadBump You might want to consider using a single method that can be overridden - that way you give control to the inheriting class how it will be done, including explicitly calling the base class code first. – tvanfosson Dec 01 '12 at 22:05
  • Sounds like a good idea. I thought of implementing the `ISerializable` interface which requires a special constructor, see [here](http://www.diranieh.com/netserialization/binaryserialization.htm#Custom%20Serialization). Will the use of constructor ensure that the base class's ctor will be called prior to derived class's serialization? – RoadBump Dec 01 '12 at 22:15
  • @RoadBump Constructors would always be called before any of the properties could be set or methods be invoked. – tvanfosson Dec 02 '12 at 04:59
  • I created a virtual method, but i got a TypeLoadException that says a serialization callback can't be virtual. What i can do? – RoadBump Dec 18 '12 at 15:58
  • I'd forgotten about that. You might not have any option to force them to call your behavior on deserialization. – tvanfosson Dec 18 '12 at 16:08
  • 1
    Ok, created a virtual function and called it from another non-virtual function marked `OnDeserialized`. Now the inheritor has a chance to call my behavior, and if he does not, he can blame himself :-). – RoadBump Dec 18 '12 at 23:22