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);
Console.WriteLine(list[100]);
}
}
}

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

Comments

  • Anonymous
    February 24, 2004
    I will try and get a small repro project as the real problem is in live code.
  • Anonymous
    February 24, 2004
    Are you using Framework 1.0 or 1.1?
  • Anonymous
    February 24, 2004
    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
    :D first place it jumps :D
  • Anonymous
    February 24, 2004
    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.
  • Anonymous
    February 24, 2004
    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.
  • Anonymous
    February 24, 2004
    I think he's talking about XML Serialization, not binary.
  • Anonymous
    February 24, 2004
    But why do they disable serialization on IDictionary based collections?
  • Anonymous
    February 24, 2004
    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.
  • Anonymous
    February 24, 2004
    Yes I was referring to XmlSerializer.

  • Anonymous
    February 24, 2004
    Whats this newfangled BinaryFormatter Serializer thang, never seen it before.
    hmm..
  • Anonymous
    February 24, 2004
    Here, try this...

    http://msdn.microsoft.com/msdnmag/issues/03/06/XMLFiles/default.aspx

    (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.
  • Anonymous
    February 24, 2004
    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.