I have to implement ISerializable in a derived class (to do some custom serialization/deserialization) but the parent class is marked as [Serializable]. The serialization "works" (I can serialize and deserialize without runtime errors) but it looks like the base class data isn't being preserved.
Is the fact that I'm implementing GetObjectData in the derived class negating the serialization of the base class? If so, do I have to implement ISerializable in the base class and then call base.GetObjectData(...) in the derived class to preserve the data or is there a better way than writing info.AddValue(...) 100 times?
edit> Thank you Tim. You confirmed what I suspected. The problem itself goes one step further. The base class in my case implements BindingList(T) which iteself doesn't implement ISerializable.
In the interim, for each property, I'll try: In the ISerializable constructor base.Property = info.GetValue(...);
and in the GetObjectDate info.AddValue("name", base.Property);
unless a better solution is put forward by the wonderful SO community.
Serializing child class if parent class does not implement serializable? it works fine but please note Employee0 still does not implement Serializable Is it mandatory for Base class has to implement Serializable to serialize the child class? If yes why its mandatory only for Employee1 but not for Employee0 ?
As per my example it looks like yes but as per other articles on net this should not be mandatory. So what i am missing here? If you want to serialize an Employee2 object, Employee2 has to implement Serializable (preferably directly rather than inheriting it).
The child class inherits the attributes and functions of its parent class. If we have sev e ral similar classes, we can define the common functionalities of them in one class and define child classes of this parent class and implement specific functionalities there.
Instance fields that are declared in a type that inherits the System.Runtime.Serialization.ISerializable interface are not automatically included in the serialization process. To include the fields, the type must implement the GetObjectData method and the serialization constructor.
do I have to implement ISerializable in the base class and then call base.GetObjectData(...) in the derived class
Yes. As soon as you implement ISerializable
, any automatic serialization is switched off.
Likewise, you'll need to implement the protected serialization constructor in both the base class and the derived class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With