[Mono-bugs] [Bug 610036] Data Contract Serializer throws exception when adding a custom dictionary to KnownTypes collection

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon May 31 05:09:21 EDT 2010


http://bugzilla.novell.com/show_bug.cgi?id=610036

http://bugzilla.novell.com/show_bug.cgi?id=610036#c1


Atsushi Enomoto <aenomoto at novell.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|Major                       |Normal

--- Comment #1 from Atsushi Enomoto <aenomoto at novell.com> 2010-05-31 09:09:20 UTC ---
A "minimum test case" could become like this:

using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections.Generic;

namespace DataContractDictionaryBug
{
    //entry point
    class MainClass
    {
        public static void Main (string[] args)
        {
            Type[] knownTypes = new Type[] { typeof(ParentClass), typeof(Foo),
typeof(Bar) };
                    var ds = new DataContractSerializer(typeof(Root), "Root",
"Company.Foo", knownTypes, 1000, false, true, null);

            var ms = new MemoryStream ();
            ds.WriteObject (ms, BuildGraph ());
            Console.WriteLine (Encoding.UTF8.GetString (ms.ToArray ()));
            ms.Position = 0;
            ds.ReadObject (ms);
        }

        static Root BuildGraph()
        {
            var FRoot = new Root("root");

            var bar1 = new Bar("bar1");
            var bar2 = new Bar("bar2");
            var bar3 = new Bar("bar3");

            var foo1 = new Foo("foo1");
            var foo2 = new Foo("foo2");

            foo1.FDict.Add(bar1);
            foo1.FDict.Add(bar2);

            foo2.FDict.Add(bar1);
            foo2.FDict.Add(bar3);

            FRoot.FDict.Add(foo1);
            FRoot.FDict.Add(foo2);
            return FRoot;
        }
    }

    //parent class with a name property
    [DataContract (Namespace = "Company.Foo")]
    public abstract class ParentClass
    {        
        //constructor
        public ParentClass(string name)
        {
            Name = name;
        }

        //the name
        [DataMember]
        public string Name{ get; set; }    
    }

    //root object
    [DataContract (Namespace = "Company.Foo")]
    public class Root : ParentClass
    {
        //dict
        [DataMember]
        public Dict<Foo> FDict;    

        //constructor
        public Root(string name)
            : base(name)
        {
            FDict = new Dict<Foo>();
        }
    }

    //subclass
    [DataContract (Namespace = "Company.Foo")]
    public class Foo : ParentClass
    {
        //here is one dict
        [DataMember]
        public Dict<Bar> FDict;

        //constructor
        public Foo(string name) 
            : base(name)
        {
            FDict = new Dict<Bar>();
        }
    }

    //another sublass
    [DataContract (Namespace = "Company.Foo")]
    public class Bar : ParentClass
    {
        //constructor
        public Bar(string name)
            : base(name)
        {
        }
    }
    //the custom dictionary
    [CollectionDataContract(ItemName = "DictItem", Namespace = "Company.Foo")]
    public class Dict<T> : Dictionary<string, T> where T : ParentClass
    {
        public void Add(T item)
        {
            Add(item.Name, item);
        }
    }
}

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


More information about the mono-bugs mailing list