Serialization inheritance

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

    #1

    Serialization inheritance

    Hi, folks!
    There is some class in external library
    that is declared with [Serializable()] attribute.
    I need to create class inherited from them
    that should be serializable too.
    My class uses fields calculated at deserialization stage,
    so it's declared not as following:
    [Serializable()]
    class MyClass : BaseClass { ... }

    but as following (look all !!! and ???'s carefully:-):

    class MyClass : BaseClass, ISerializable
    {
    protected int SimpleField; // serializable
    protected int CalculatedField ; // non-serializable

    public MyClass(
    SerializationIn fo info,
    StreamingContex t ctx)
    // : base(info, ctx) // error!!!
    {
    // my stuff
    info.Add("Simpl eField", SimpleField);
    }
    public virtual void GetObjectData(
    SerializationIn fo info,
    StreamingContex t ctx)
    {
    // base.GetObjectD ata(info, ctx); // error!!!
    // my stuff
    SimpleField = info.GetInteger ("SimpleField") ;
    // how to conform next line
    // with calling of inherited deserialization routine???
    CalculatedField = SomethingVerySp ecial(SimpleFie ld);
    }
    }

    Well, I don't know how to call inherited constructor
    and GetObjectData from my own constructor/GetObjectData
    appropriately, because this functionality in base class
    is generated implicitly and cannot be called by name.

    How to solve this problem?

    WBR, Ilya


  • Vadym Stetsyak

    #2
    Re: Serialization inheritance

    [color=blue]
    > Well, I don't know how to call inherited constructor
    > and GetObjectData from my own constructor/GetObjectData
    > appropriately, because this functionality in base class
    > is generated implicitly and cannot be called by name.
    >
    > How to solve this problem?[/color]

    you can use ConstuctorInfo class from System.Reflecti on namespace with the
    GetConstructor method


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Serialization inheritance

      Ilya,
      One of the following articles covers how to have a derived class implement
      ISerializable, while the base class does not.

      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/

      Note I find all three articles invaluable when working with .NET
      serialization.

      Hope this helps
      Jay


      "Ilya Evseev" <evseev@csa.r u> wrote in message
      news:3FC67AE0.C A842CAF@csa.ru. ..[color=blue]
      > Hi, folks!
      > There is some class in external library
      > that is declared with [Serializable()] attribute.
      > I need to create class inherited from them
      > that should be serializable too.
      > My class uses fields calculated at deserialization stage,
      > so it's declared not as following:
      > [Serializable()]
      > class MyClass : BaseClass { ... }
      >
      > but as following (look all !!! and ???'s carefully:-):
      >
      > class MyClass : BaseClass, ISerializable
      > {
      > protected int SimpleField; // serializable
      > protected int CalculatedField ; // non-serializable
      >
      > public MyClass(
      > SerializationIn fo info,
      > StreamingContex t ctx)
      > // : base(info, ctx) // error!!!
      > {
      > // my stuff
      > info.Add("Simpl eField", SimpleField);
      > }
      > public virtual void GetObjectData(
      > SerializationIn fo info,
      > StreamingContex t ctx)
      > {
      > // base.GetObjectD ata(info, ctx); // error!!!
      > // my stuff
      > SimpleField = info.GetInteger ("SimpleField") ;
      > // how to conform next line
      > // with calling of inherited deserialization routine???
      > CalculatedField = SomethingVerySp ecial(SimpleFie ld);
      > }
      > }
      >
      > Well, I don't know how to call inherited constructor
      > and GetObjectData from my own constructor/GetObjectData
      > appropriately, because this functionality in base class
      > is generated implicitly and cannot be called by name.
      >
      > How to solve this problem?
      >
      > WBR, Ilya
      >
      >[/color]


      Comment

      Working...