Inheriting Serialization

M

Maheal

I have been trying to Serialize an object that is the child of another
object which is also serializable. Here is the simplified scenario
(assume missing code is correct):


class One : ISerializable {
int x;
int y;
One() {}; // constructor
One(SerializationInfo i, StreamingContext c) { // deserialization
constructor
// deserialize x and y
};
GetObjectData(SerializationInfo i, StreamingContext c) {
// serialize x and y
};
}

class Two : One, ISerializable {
int z;
One() {}; // constructor
One(SerializationInfo i, StreamingContext c) { // deserialization
constructor
// deserialize z
};
GetObjectData(SerializationInfo i, StreamingContext c) {
// serialize z
};
}


Here's the problem... I can serialize class One just fine, it saves x
and y and reloads it. However class Two returns a compile warning,
"The keyword new is required on Two.GetObjectData() because it hides
inherited member One.GetObjectData()" Whether I add "new" or not
doesn't matter, it only serializes "z", not "x" and "y". I have found
that calling base.GetObjectData() is an easy way around this. But,
deserialization has the same problem. It will use the deserialization
constructor for Two, but the regular constructor for its parent, One.

It seems there should be an easy way to get serialization to follow
the laws of inheritance. I'm not about to write duplicate code for
each child of One. It's tedious and bad programming....what if I
changed One? Is there anyway to get Serialization to work this way?

Maheal
 
J

Jay B. Harlow [MVP - Outlook]

Maheal,
Define the GetObjectData as virtual in the base class, then override it in
the derived class. Being certain to call the base version!

Also, I normally make both the special constructor & the GetObjectData
method as protected, as only the serialization process is interested in
them...

Something like:

class One : ISerializable
{
int x;
int y;

public One()
{
// constructor
}

protected One(SerializationInfo i, StreamingContext c)
{ // deserialization constructor
// deserialize x and y
}

protected virtual void GetObjectData(SerializationInfo i,
StreamingContext c)
{
// serialize x and y
}

void ISerializable.GetObjectData(SerializationInfo i,
StreamingContext c)
{
GetObjectData(i, c);
}
}

class Two : One
{
int z;

public Two()
{
// constructor
}

protected Two(SerializationInfo i, StreamingContext c) : base(i, c)
{ // deserialization constructor
// deserialize z
}

protected override void GetObjectData(SerializationInfo i,
StreamingContext c)
{
base.GetObjectData(i, c);
// serialize z
}
}

The following articles provide a wealth of information on serialization:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top