472,378 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 4373
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: VicenÁ Masanas | last post by:
I want to specialize XmlElementAttribute so I can control how an specific property is serialized, but I cannot get what methods should I override to accomplish this. <Serializable()> Public...
2
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
2
by: Scott Reynolds | last post by:
I am having a problem exposing a class inherited from the collection base class as a webservice. If I expose the collection on a web page all works well and I am very happy. However when I try and...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
1
by: Lucile | last post by:
Hi to All, I am new to this newsgroup, so please bare with me. Here is the deal: I have the following Object Graph: Icell (Interface) Iline (Interface) Itable (Interface) Classes: Cell:...
4
by: Varangian | last post by:
Hello something I want to ask...... why on earth you cannot inherit from XmlElement???? .... or you can but XmlElement constructor is protected or private and so you cannot define an empty...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.