[Mono-bugs] [Bug 419973] New: XML Serialization of derived classes gives exception

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon Aug 25 04:52:18 EDT 2008


           Summary: XML Serialization of derived classes gives exception
           Product: Mono: Class Libraries
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Major
          Priority: P5 - None
         Component: Sys.XML
        AssignedTo: atsushi at ximian.com
        ReportedBy: josef.semmler at gmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

Description of Problem:
I have a use case, where i'd like to serialized instances of a derived class.
To support this i implemented a class which can serialized derived classes. The
code is below. The code works with microsoft .net without problems - but
results in exceptions using mono.

Steps to reproduce the problem:
1. compile the attached code
2. run it 

Actual Results:

Unhandled Exception: System.InvalidOperationException: There was an error
generating the XML document. ---> System.InvalidCastException: Cannot cast from
source type to destination type.
(System.Xml.Serialization.XmlTypeMapElementInfo elem, System.Object
memberValue) [0x00000]
(System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList)

Expected Results:
a well formated XML representation of the serialized class

How often does this happen? 

Additional Information:
the idea behind the code was taken from

Here is the code:

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace SerialMonoBug
    class Program
        static void Main(string[] args)
            String s = "";

            TestClass a = new TestClass();
            DerivedClass_1 d1 = new DerivedClass_1();

            a.Item = d1;
            s = a.ToString_2();
System.Console.WriteLine(String.Format("{0}\n------------------------\n", s));

    public class UnknownItemSerializer : IXmlSerializable
        public BaseClass Item;

        public UnknownItemSerializer() { }
        public UnknownItemSerializer(BaseClass item) { this.Item = item; }

        public static implicit operator UnknownItemSerializer(BaseClass p) {
            System.Console.WriteLine("converting to UnknownItemSerialzer");
            return p == null ? null : new UnknownItemSerializer(p);

        public static implicit operator BaseClass(UnknownItemSerializer p) {
            System.Console.WriteLine("converting to BaseClass");
            return p == null ? null : p.Item;

        public XmlSchema GetSchema() { return null; }// not implemented
        public void ReadXml(XmlReader reader) { return; } // not implemented

        public void WriteXml(XmlWriter writer)
            writer.WriteAttributeString("type", String.Format("{0}, {1}",
Item.GetType().ToString(), Item.GetType().Assembly.ToString()));
            new XmlSerializer(Item.GetType()).Serialize(writer, Item);

    public class TestClass
        public BaseClass Item;

        public string ToString_2() {
            string result = "";

            using (System.IO.MemoryStream stream = new
            using (System.IO.StreamReader sr = new
            using (XmlTextWriter writer = new XmlTextWriter(stream,
                XmlAttributes attrs = new XmlAttributes();
                XmlElementAttribute attr = new XmlElementAttribute();
                attr.ElementName = "UnknownItemSerializer";
                attr.Type = typeof(UnknownItemSerializer);
                XmlAttributeOverrides attrOverrides = new
                attrOverrides.Add(typeof(TestClass), "Item", attrs);

                XmlSerializer serializer = new XmlSerializer(this.GetType(),

                serializer.Serialize(writer, this);

                stream.Position = 0;
                result = sr.ReadToEnd();

            return result;

    public class BaseClass { 

    public class DerivedClass_1 : BaseClass {
        public int value = 10;

    public class DerivedClass_2 : BaseClass {
        public string strValue = "testString";

Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list