Error: The type is not supported because it implements IDictionary.

Ferris Beuller asked me in response to my previous post:

Why can the serializer not serialize types that implement IDictionary? SortedList in types cannot be serialized, you get an InvalidOperationException with the innerexception of {“The type System.Collections.SortedList is not supported because it implements IDictionary.”}

The serializer should be able to serialize any type as long as it is marked serializable. SortedList is marked serializable. So, you should not be getting this error. I was able to run the following test without getting any errors.

using System;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace SerializeDict
    public class Test
        public static void Main()
            MemoryStream stream = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            SortedList list = new SortedList();
            list.Add(10, 100);
            list.Add(100, 1000);
            bf.Serialize(stream, list);
            stream.Position = 0;
            bf = new BinaryFormatter();
            list = (SortedList)bf.Deserialize(stream);


Can you share a reproducible sample with me where you get the error?

Comments (12)

  1. Ferris Beuller says:

    I will try and get a small repro project as the real problem is in live code.

  2. Santhosh Pillai says:

    Are you using Framework 1.0 or 1.1?

  3. Ferris Beuller says:

    Until then I would isolate where you are throwing those exceptions in the actual serializer to see what conditions would causé that because I am obviously hitting them.

    I would also check for comments like

    // This should not happen

    😀 first place it jumps 😀

  4. Ferris Beuller says:

    Visual Studio .NET 2003 which I would imagine is 1.1, right?

    Once I have a repro scenario on that IDE I will try it on my home 2002 edition.

  5. Ferris Beuller says:

    Visual Studio .NET 2003 which I would imagine is 1.1, right?

    Once I have a repro scenario on that IDE I will try it on my home 2002 edition.

  6. Julien Ellie says:

    I think he’s talking about XML Serialization, not binary.

  7. Ferris Beuller says:

    But why do they disable serialization on IDictionary based collections?

  8. JosephCooney says:

    Yes – I think he is talking about XML serialization too. IIRR I’ve been able to serialize plenty of things under framework 1.0 and 1.1.

  9. Ferris Beuller says:

    Yes I was referring to XmlSerializer.

  10. Ferris Beuller says:

    Whats this newfangled BinaryFormatter Serializer thang, never seen it before.


  11. Johnny Hall says:

    Here, try this…

    (2/3 of the way down)

    You can serialize IDictionary but you need to tell the serializer what the format of the XML should be by implementing IXmlSerializable.

  12. Johnny Hall says:

    I believe it is because the XmlSerializer has no way to know what format to serialize a dictionary as, because there is no equivalent type in XML Schema.

    The only way to do it would have been to "hard-code" an XML format into IDictionary, or into the XmlSerializer code itself.

    And you have to remember that XmlSerializer was (I believe) added to the framework late on, and that it started out as someone’s personal project. Though don’t quote me on that, I could be wrong.