[Mono-bugs] [Bug 695203] New: KnownType for Derived Class not Resolved

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Fri May 20 13:24:40 EDT 2011


https://bugzilla.novell.com/show_bug.cgi?id=695203

https://bugzilla.novell.com/show_bug.cgi?id=695203#c0


           Summary: KnownType for Derived Class not Resolved
    Classification: Mono
           Product: Mono: Class Libraries
           Version: 2.10.x
          Platform: x86
        OS/Version: openSUSE 11.3
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: WCF
        AssignedTo: atsushi at ximian.com
        ReportedBy: cristiano.simionato at cocai.eu
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---


User-Agent:       Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101
Firefox/4.0.1

In a hierarchy of classes the base class has to have all KnownTypeAttribute of
the derived and not olny the direct derived.

In this example code, the second serialization throw an SerializationException
of type unexpected, but in .Net all run correctly.

class Program
{
    static void Main()
    {
        using (var mem = new MemoryStream())
        {
            BaseClass data = new DerivedA1 { Code = "1", CodeA = "A", CodeA1 =
"A1" };
            Serialize(data, mem);
            mem.Position = 0;
            var docResult = Deserialize<BaseClass>(mem);
        }

        using (var mem = new MemoryStream())
        {
            BaseClass data = new DerivedA2 { Code = "1", CodeA = "A", CodeA2 =
"A1" };

            ///////////////////////////////////////////////
            // The next serialization throw an SerializationException
            ///////////////////////////////////////////////
            Serialize(data, mem);


            mem.Position = 0;
            var docResult = Deserialize<BaseClass>(mem);
        }
    }


    public static void Serialize<T>(T instance, Stream destinationStream)
    {
        var serializer = new DataContractSerializer(typeof(T), null,
int.MaxValue, false, true, null);

        using (var writer =
XmlDictionaryWriter.CreateBinaryWriter(destinationStream, null, null, false))
        {
            serializer.WriteObject(writer, instance);
        }
    }


    public static T Deserialize<T>(Stream sourceStream)
    {
        var serializer = new DataContractSerializer(typeof(T), null,
int.MaxValue, false, true, null);

        using (var reader =
XmlDictionaryReader.CreateBinaryReader(sourceStream,
XmlDictionaryReaderQuotas.Max))
        {
            return (T)serializer.ReadObject(reader);
        }
    }
}

[DataContract]
[KnownType(typeof(DerivedA1))]
[KnownType(typeof(DerivedA))]
public abstract class BaseClass
{
    [DataMember]
    public string Code { get; set; }
}


[DataContract]
[KnownType(typeof(DerivedA1))]
[KnownType(typeof(DerivedA2))]
public abstract class DerivedA : BaseClass
{
    public string CodeA { get; set; }
}

[DataContract]
public class DerivedA1 : DerivedA
{
    [DataMember]
    public string CodeA1 { get; set; }
}

[DataContract]
public class DerivedA2 : DerivedA
{
    [DataMember]
    public string CodeA2 { get; set; }
}

Reproducible: Always

Steps to Reproduce:
1. Create an Application Console with the code of the Details targeting .Net4
2. Run the application
3. the second serialization throw the exception.
Actual Results:  
System.Runtime.Serialization.SerializationException has been thrown
"Type 'MyNameSpace.DerivedA2' is unexpected. The type should either be
registered as known type, or DataContractResolver should be used."

Stack Trace:
  at System.Runtime.Serialization.DataContractSerializer.WriteStartObject
(System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00137] in
/usr/src/packages/BUILD/mono-2.10.2/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/DataContractSerializer.cs:463 
  at System.Runtime.Serialization.XmlObjectSerializer.WriteObject
(System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00000] in
/usr/src/packages/BUILD/mono-2.10.2/mcs/class/System.Runtime.Serialization/System.Runtime.Serialization/XmlObjectSerializer.cs:106 
  at MyNameSpace.Program.Serialize[BaseClass] (MyNameSpace.BaseClass instance,
System.IO.Stream destinationStream) [0x00023] in
/home/xxxx/Projects/MyNameSpace/MyNameSpace/Program.cs:38 
  at MyNameSpace.Program.Main () [0x0008d] in
/home/xxxx/Projects/MyNameSpace/MyNameSpace/Program.cs:25 

Expected Results:  
None exception.

Mono update to last 10.2 stable.

-- 
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