How to force serialize as base type?

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

    How to force serialize as base type?

    Here's the scenario (public attributes, etc. omitted for brevity):

    class Base
    {
    }

    class Derived : Base
    {
    }

    class Container : List<Base>
    {
    }

    class Something
    {
    Container Contents = new Container();
    }


    //...

    Something thing = new Something();
    thing.Contents. Add(new Derived());

    XmlSerializer serializer = new XmlSerializer(t ypeof(Something ));
    serializer.Seri alize(destinati onFile, thing);



    What I need to do is to force the serialization of the container types
    as type Base, I don't want Derived in the xml. Is there a way to do this?
  • Wen Yuan Wang [MSFT]

    #2
    RE: How to force serialize as base type?

    Hello Julie,

    According to your description, you want to force the serialization of the
    derived class as its base type, correct? if I misunderstood anything here,
    please don't hesitate to correct me.

    I'm afraid that will be very difficult. As far as I know, Serialization
    checks type of instance by calling Object.getType( ) method. This method
    always returns the exact type of object. I tried to cast Devired instance
    as Base type, but it will failed on serializing.

    Derived d = new Derived();
    Base b = (Base)d;
    XmlSerializer serializer = new XmlSerializer(t ypeof(Base));
    serializer.Seri alize(Console.O ut, b);

    What we can do is to lement IXmlSerializabl e interface, and do outputing
    xml by ourself. But there will be a lot of work to do.
    Gain technical skills through documentation and training, earn certifications and connect with the community

    izable.aspx
    [IXmlSerializabl e Interface]

    Could you let me know why do you want to serialize your derived instance as
    base type? In serialization, we would like to output all the information of
    instance to xml file. If you serialize the object as base type, members
    defined in derived class will be lost. Is this what you need? In my
    opinion, if you don't want to output some members into xml file, you can
    define them as NonSerialized. This attribute indicates that a field of a
    serializable class should not be serialized.
    Indicates that a field of a serializable class should not be serialized. This class cannot be inherited.

    [NonSerializedAt tribute Class]

    Hope this helps. Please feel free to let me know if you have any more
    concern. We are glad to assist you.

    Have a great day,
    Best regards,
    Wen Yuan

    Microsoft Online Community Support
    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.
    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    Gain technical skills through documentation and training, earn certifications and connect with the community

    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscripti...t/default.aspx.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Julie

      #3
      Re: How to force serialize as base type?

      Wen Yuan Wang [MSFT] wrote:
      Hello Julie,
      >
      According to your description, you want to force the serialization of the
      derived class as its base type, correct? if I misunderstood anything here,
      please don't hesitate to correct me.
      >
      I'm afraid that will be very difficult. As far as I know, Serialization
      checks type of instance by calling Object.getType( ) method. This method
      always returns the exact type of object. I tried to cast Devired instance
      as Base type, but it will failed on serializing.
      >
      Derived d = new Derived();
      Base b = (Base)d;
      XmlSerializer serializer = new XmlSerializer(t ypeof(Base));
      serializer.Seri alize(Console.O ut, b);
      >
      What we can do is to lement IXmlSerializabl e interface, and do outputing
      xml by ourself. But there will be a lot of work to do.
      Gain technical skills through documentation and training, earn certifications and connect with the community

      izable.aspx
      [IXmlSerializabl e Interface]
      >
      Could you let me know why do you want to serialize your derived instance as
      base type? In serialization, we would like to output all the information of
      instance to xml file. If you serialize the object as base type, members
      defined in derived class will be lost. Is this what you need? In my
      opinion, if you don't want to output some members into xml file, you can
      define them as NonSerialized. This attribute indicates that a field of a
      serializable class should not be serialized.
      Indicates that a field of a serializable class should not be serialized. This class cannot be inherited.

      [NonSerializedAt tribute Class]
      >
      Hope this helps. Please feel free to let me know if you have any more
      concern. We are glad to assist you.
      Yes, you understand what I'm after. I'll look into the NonSerialized
      attribute to see if I can get it to work.

      The reason that I need to do this is essentially due to a client/server
      relationship between the data:

      The server understands the base (and that is all the information it
      needs to carry out its business, AND I don't have ultimate latitude to
      modify server code at this point);

      The client, on the other hand, uses a derived class to manage the
      additional data that it needs on its side.

      The solution that I've come up so far is to essentially implement a
      clone method on the base class and then serialize the clone. It works,
      but isn't elegant or all that self-commenting in code...

      Thanks for your help.

      Comment

      • Wen Yuan Wang [MSFT]

        #4
        Re: How to force serialize as base type?

        Hello Julie,
        Thanks for your reply.

        How did you deserialize the data on server? I'm afriad you may receive
        error message said "The Derived type was not recognized" if you deserialize
        xml string as base type. "NonSeriali zed" attribute may not help on such
        senario.

        We have to use the same XmlSerializer to serialize/deserialize data.
        In my opnion, you may have to covert your derived object into base type
        before serializing. Just as what you have done, implement a clone mehod to
        copy required information into base instance, and then replace all items in
        List with its base type object before serializing.

        Something thing = new Something();
        ......

        Container c = new Container();
        foreach (Base b in thing.Contents)
        {
        c.Add(b.Clone() );
        }
        thing.Contents = c;

        XmlSerializer serializer = new XmlSerializer(t ypeof(Something ));

        serializer.Seri alize(Console.O ut, thing);

        Hope this helps. Please feel free to let us know if you have any more
        concern. We are glad to assist you.
        Have a great day,
        Best regards,
        Wen Yuan

        Microsoft Online Community Support
        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        Working...