views:

433

answers:

1

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.

+2  A: 

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.

Tim Robinson
Very good info, but what do I do if my base class itself inherits from BindingList(T) - which does not implement ISerializable (but is marked as [Serializable])?
SnOrfus
In the past I've tackled this by adding code to my own serialization constructor and GetObjectData method.
Tim Robinson
yea, as per my edit, I think that's what I've decided to do as well. I was just hoping for a more elegant solution than this.
SnOrfus