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.