up cast to base

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff.Boeker@gmail.com

    up cast to base

    Hello,

    I have a base class that is serializable and a derived class that is
    not. I want to pass the base class object to an external function that
    requires a serializable object. How can I cleanly create a new
    instance of the base class that copies the base class from the derived
    object. I can use brute force and create a new function that copies
    each member variable but I assume there's a better way.

    CDerived derived;
    CBase base = derived as CBase;
    obj.Serialize(b ase); ->error not serializable

    Thanks,
    Jeff

  • Marc Vangrieken

    #2
    Re: up cast to base

    I have a base class that is serializable and a derived class that is
    not.
    How did you achieve this? Are you getting errors at compile- or
    runtime? Did you use the serializable attribute on your baseclass? Or
    did you implement GetObjectData from ISerializable in your baseclass? I
    think the latter approach wil work and would be an alternative to
    creating a baseclass and copying all the values.

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: up cast to base

      Hi,

      If the base class is serializable ( it implement ISerializable ) then the
      derived class is too.

      You can cast the derived instance to the base class:

      externalmethod( (BaseClass) derived_instanc e)

      <Jeff.Boeker@gm ail.comwrote in message
      news:1161247774 .179654.249430@ b28g2000cwb.goo glegroups.com.. .
      Hello,
      >
      I have a base class that is serializable and a derived class that is
      not. I want to pass the base class object to an external function that
      requires a serializable object. How can I cleanly create a new
      instance of the base class that copies the base class from the derived
      object. I can use brute force and create a new function that copies
      each member variable but I assume there's a better way.
      >
      CDerived derived;
      CBase base = derived as CBase;
      obj.Serialize(b ase); ->error not serializable
      >
      Thanks,
      Jeff
      >

      Comment

      • JBoeker@gmail.com

        #4
        Re: up cast to base

        My base class is serializable by attribute, i.e. [Serialiazble]
        The run time error I get when calling the external function is "Type
        CDerived' in Assembly 'Test, Version=1.0.0.0 , Culture=neutral ,
        PublicKeyToken= null' is not marked as serializable".

        I guess the external function is still looking at the derived class.
        Casting when calling doesn't help. I implemented a copy constructor in
        the base class and use that instance and it works but ideally I would
        not have to create a copy.

        Jeff
        Ignacio Machin ( .NET/ C# MVP ) wrote:
        Hi,
        >
        If the base class is serializable ( it implement ISerializable ) then the
        derived class is too.
        >
        You can cast the derived instance to the base class:
        >
        externalmethod( (BaseClass) derived_instanc e)
        >
        <Jeff.Boeker@gm ail.comwrote in message
        news:1161247774 .179654.249430@ b28g2000cwb.goo glegroups.com.. .
        Hello,

        I have a base class that is serializable and a derived class that is
        not. I want to pass the base class object to an external function that
        requires a serializable object. How can I cleanly create a new
        instance of the base class that copies the base class from the derived
        object. I can use brute force and create a new function that copies
        each member variable but I assume there's a better way.

        CDerived derived;
        CBase base = derived as CBase;
        obj.Serialize(b ase); ->error not serializable

        Thanks,
        Jeff

        Comment

        Working...