Inheriting Serialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maheal

    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(Serializati onInfo i, StreamingContex t c) { // deserialization
    constructor
    // deserialize x and y
    };
    GetObjectData(S erializationInf o i, StreamingContex t c) {
    // serialize x and y
    };
    }

    class Two : One, ISerializable {
    int z;
    One() {}; // constructor
    One(Serializati onInfo i, StreamingContex t c) { // deserialization
    constructor
    // deserialize z
    };
    GetObjectData(S erializationInf o i, StreamingContex t 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.GetObjectDa ta() because it hides
    inherited member One.GetObjectDa ta()" Whether I add "new" or not
    doesn't matter, it only serializes "z", not "x" and "y". I have found
    that calling base.GetObjectD ata() 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
  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Inheriting Serialization

    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(Serializati onInfo i, StreamingContex t c)
    { // deserialization constructor
    // deserialize x and y
    }

    protected virtual void GetObjectData(S erializationInf o i,
    StreamingContex t c)
    {
    // serialize x and y
    }

    void ISerializable.G etObjectData(Se rializationInfo i,
    StreamingContex t c)
    {
    GetObjectData(i , c);
    }
    }

    class Two : One
    {
    int z;

    public Two()
    {
    // constructor
    }

    protected Two(Serializati onInfo i, StreamingContex t c) : base(i, c)
    { // deserialization constructor
    // deserialize z
    }

    protected override void GetObjectData(S erializationInf o i,
    StreamingContex t c)
    {
    base.GetObjectD ata(i, c);
    // serialize z
    }
    }

    The following articles provide a wealth of information on serialization:

    Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

    Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.

    Learn with interactive lessons and technical documentation, earn professional development hours and certifications, and connect with the community.


    Hope this helps
    Jay

    "Maheal" <maheal.geo@yah oo.com> wrote in message
    news:85929bb2.0 310020843.3d922 7ee@posting.goo gle.com...[color=blue]
    > 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(Serializati onInfo i, StreamingContex t c) { // deserialization
    > constructor
    > // deserialize x and y
    > };
    > GetObjectData(S erializationInf o i, StreamingContex t c) {
    > // serialize x and y
    > };
    > }
    >
    > class Two : One, ISerializable {
    > int z;
    > One() {}; // constructor
    > One(Serializati onInfo i, StreamingContex t c) { // deserialization
    > constructor
    > // deserialize z
    > };
    > GetObjectData(S erializationInf o i, StreamingContex t 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.GetObjectDa ta() because it hides
    > inherited member One.GetObjectDa ta()" Whether I add "new" or not
    > doesn't matter, it only serializes "z", not "x" and "y". I have found
    > that calling base.GetObjectD ata() 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[/color]


    Comment

    Working...