Dictionary container serialization problems.

I have a dictionary datamember that I am trying to serialize and get the following error

{"Cannot serialize member InstrumentUI.Tallie.UserSpecificData of type System.Collections.Generic.Dictionary`2[[System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[InstrumentUI.Tallie+UserTally, InstrumentUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], because it implements IDictionary."}

Does this mean that you cannot serialize the dictionary data member. And if so is there antoher container that I could use. I need a data member that can store a byte[] as the key and hold another datamember that stores a string as the key and an in for the value. any ideas. I was using the dictionary like this

dictionary<byte [] , dictionary <string, int> >.

Anything here would be very helpful. Thanks, Matt



Answer this question

Dictionary container serialization problems.

  • INTPnerd

    The soap formatter does not support generic data types in .net 2.0 I am stuck between a rock and a hard place. Is there no way out of this one
  • ti_m

    What I ended up doing is converting it to a public list of key value paits value by value.  I had problems deserialising the list though so I converted the list to array's.  It was not to hard and it seemed to fit the best for the situation.  I think it is silly though that the dictionary data member is not serializable.  Why isn't it   It doesn't seem like it would be that dificult to impliment the functionality into the serialize functions.  Maybe someone could explain to me why it can't be serialized.

     If anyone is interested on the code that was used to convert the dictionary to key pair lists and then to array's let me know and I'll see what I can scrounge up.


  • Noorul Ahmed

    Does anybody know of an example of this code written in vb.net

    Thank you


  • friendz

    How are you trying to serialize the IDictionary object   If you're using XmlSerializer, it doesn't support IDictionary.

  • Brad Mittelstedt

    Its bafflingly annoying that IDictionary objects are not serialzable by default since it seems pretty easy to implement. You can however derive from a dictionary and add the functionality. Here is an elegant example:

    http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

  • Rumen Filkov

    mattdawg wrote:
    I am using XmlSerializer. What other alternative should I use
    You could try using SoapFormatter to generate raw XML to dump out to your stream.

  • Jamie LJ

    I am using XmlSerializer. What other alternative should I use
  • ravi_nilesh

    This question has come up a few times on internal discussion threads recently so I wanted to summarize some highlights. It's correct that you get this error because Dictionary isn't IXmlSerializable. The reason it's this way is layering concerns: Dictionary is in mscorlib.dll but IXmlSerializable is in System.Xml.dll, and we couldn't take a dependency in that direction.

    Before .NET 3.0 you'd have to grow your own solution, such as subclassing Dictionary to implement IXmlSerializable. Moving forward, the recommended solution is to use System.Runtime.Serialization.DataContractSerializer. Note that this is part of the Windows Communication Foundation (WCF), in .NET 3.0. For more details on what is serializable using the data contract serializer, see: http://msdn2.microsoft.com/en-us/library/ms731923.aspx


  • Paul Fuhrmann

    All I have is the system.Runtime.Serialization.Formatters.Binary How do you get the SoapFormatter
  • kilo94

    Ok I was just being stupid I was adding the references to the wrok application. Ok I added the system.runtime.serialization.formatters.soap to the application and I can now creat SoapFormatters.
  • Dictionary container serialization problems.