Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

How to force serialize as base type?

Question posted by: Julie (Guest) on June 27th, 2008 07:20 PM
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(typeof(Something));
serializer.Serialize(destinationFile, 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]'s Avatar
Wen Yuan Wang [MSFT]
Guest
n/a Posts
June 27th, 2008
07:20 PM
#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(typeof(Base));
serializer.Serialize(Console.Out, b);

What we can do is to lement IXmlSerializable interface, and do outputing
xml by ourself. But there will be a lot of work to do.
http://msdn2.microsoft.com/en-us/li...tion.ixmlserial
izable.aspx
[IXmlSerializable 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.
http://msdn2.microsoft.com/en-us/li...dattribute.aspx
[NonSerializedAttribute 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:
Join Bytes!.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
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/subscript...rt/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Julie's Avatar
Julie
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: How to force serialize as base type?
Wen Yuan Wang [MSFT] wrote:
Quote:
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(typeof(Base));
serializer.Serialize(Console.Out, b);
>
What we can do is to lement IXmlSerializable interface, and do outputing
xml by ourself. But there will be a lot of work to do.
http://msdn2.microsoft.com/en-us/li...tion.ixmlserial
izable.aspx
[IXmlSerializable 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.
http://msdn2.microsoft.com/en-us/li...dattribute.aspx
[NonSerializedAttribute 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.

Wen Yuan Wang [MSFT]'s Avatar
Wen Yuan Wang [MSFT]
Guest
n/a Posts
June 27th, 2008
07:20 PM
#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. "NonSerialized" 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(typeof(Something));

serializer.Serialize(Console.Out, 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:
Join Bytes!.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


 
Not the answer you were looking for? Post your question . . .
190,472 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors