Heirarchical serialization

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

    #1

    Heirarchical serialization

    I haven't used Serialization support in .NET before and I have a few
    questions about it.

    let's assume the following:

    class A : ISerializable
    {
    ....
    }

    class B : A, ISerializable
    {
    private C cField;
    ....
    }


    class C: ISerializable
    {
    ....
    }


    First of all, yeah, I'm missing the Serializable attributes here, and a lot
    of other stuff, but this should be sufficient for my question..

    In class B, how do I define GetObjectData() ? I need to define it as new or
    override? If it's "new" then from B.GetObjectData () do I simply call
    base.GetObjectD ata() to deserialize the base class A? Does that work in a
    "new" method?

    What about the class C which is contained within B? In the
    B(Serialization Info, StreamingContex t) constructor, do I simply do a cField
    = new C(serInfo, context)? In B.GetObjectData () do I just call
    cField.GetObjec tData(serInfo, context)

    Just not really sure how this all goes together. The above seems to make
    sense to me except for whether or not B.GetObjectData should be new or
    override. Of course, I could be completely off on all of it.

    The other question is in regards to classes that aren't serializable. For
    example, if I have a member that is a Font class, and I want to serialize
    it, do I just take all of the pertinent data from the font object and
    serialize those pieces that I need to re-create the font in the serializable
    constructor?

    Thanks

    Pete



  • Joe Mayo

    #2
    Re: Heirarchical serialization


    "Pete Davis" <pdavis68@hotma il.com> wrote in message
    news:a151371f4b ab011e43824d9bb 34086bc@news.me ganetnews.com.. .[color=blue]
    > I haven't used Serialization support in .NET before and I have a few
    > questions about it.
    >
    > let's assume the following:
    >
    > class A : ISerializable
    > {
    > ...
    > }
    >
    > class B : A, ISerializable
    > {
    > private C cField;
    > ...
    > }
    >
    >
    > class C: ISerializable
    > {
    > ...
    > }
    >
    > In class B, how do I define GetObjectData() ? I need to define it as new or
    > override? If it's "new" then from B.GetObjectData () do I simply call
    > base.GetObjectD ata() to deserialize the base class A? Does that work in a
    > "new" method?[/color]

    A should be virtual and B and other derived types should be override. Be
    sure to call the base method after serializing members.

    public override void GetObjectData(
    SerializationIn fo info,
    StreamingContex t context )
    {
    info.AddValue(" cField", cField, typeof(C));

    base.GetObjectD ata(info,contex t);
    }

    [color=blue]
    > What about the class C which is contained within B? In the
    > B(Serialization Info, StreamingContex t) constructor, do I simply do a[/color]
    cField[color=blue]
    > = new C(serInfo, context)? In B.GetObjectData () do I just call
    > cField.GetObjec tData(serInfo, context)[/color]

    By calling it in B.GetObjectData , as in the example above, the serializer
    will call C.GetObjectData for you.
    [color=blue]
    > The other question is in regards to classes that aren't serializable. For
    > example, if I have a member that is a Font class, and I want to serialize
    > it, do I just take all of the pertinent data from the font object and
    > serialize those pieces that I need to re-create the font in the[/color]
    serializable[color=blue]
    > constructor?[/color]

    You have the right idea.

    Joe
    --
    Learn C# with step-by-step tutorials and practical examples. Start your first program, explore lists and parsing, and understand how .NET runs your code.



    Comment

    Working...