469,568 Members | 1,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

Inheriting Serialization

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
Nov 15 '05 #1
1 4261
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

"Maheal" <ma********@yahoo.com> wrote in message
news:85**************************@posting.google.c om...
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

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by VicenÁ Masanas | last post: by
3 posts views Thread by Aaron Clamage | last post: by
4 posts views Thread by Varangian | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.