Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I implement ISerializable in a child class, does the parent have to as well?

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.

like image 401
Steven Evers Avatar asked May 06 '09 17:05

Steven Evers


People also ask

Can I serialize child class if parent class does not implement Serializable?

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 ?

Is it mandatory to implement serialization of employee2 object?

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).

What is the difference between parent and child classes?

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.

Which fields are not included in the serialization process?

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.


1 Answers

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.

like image 189
Tim Robinson Avatar answered Oct 04 '22 08:10

Tim Robinson