473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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(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?
Jun 27 '08 #1
3 10199
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/lib...ion.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/lib...attribute.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:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #2
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(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/lib...ion.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/lib...attribute.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.
Jun 27 '08 #3
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:
ms****@microsoft.com.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott.ballard | last post by:
Greetings, I am using the .Net Framework XmlSerializer to serialize a class that contains some string properties. The problem is that when a string contains a zero-length string ("") it is...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
0
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
3
by: Pol Bawin | last post by:
A class has a private field of type IWizard (An interface) and a public property to access it. When I try to serialize the Geometry class in XML, i have an error but it works in Binary Can...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
0
by: Romain TAILLANDIER | last post by:
Hi group I am quite new to xml. I have a strong typed collection derived from a collection base, and i need to serialize it. I have seriously search on the net and don't find any help about how...
0
by: phobos7 | last post by:
Hi, I'm looking for a way to force a derived class to serialise/serialize as its base class. I was hoping that this was possible by just applying an attribute to the the sub class. public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.