Page 1 of 1

Dictionary<> serialization

Posted: Tue Jun 27, 2017 5:50 am
by kmatt
Is the Dictionary supposed to be able to serialize? I don't see it listed as not being supported on XmlSerializer Limitations:

[DataContract]
public class IPCGlobalConfiguration
{
[DataMember]
public Dictionary<int, string> StandbyServers { get; set; }
.....
I get an error:
Exception thrown: 'System.InvalidOperationException' in System.Xml.dll:

"Cannot serialize member SharedObjects.IPCGlobalConfiguration.StandbyServers of type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary."

Re: Dictionary<> serialization

Posted: Sat Aug 19, 2017 5:58 am
by JS-Support @Userware
Hi,

Indeed, the Dictionary class cannot be serialized with the XmlSerializer. We have just added it to the list of limitations.

To work around this issue, you can create a property of type List<KeyValuePair<T1,T2>> or List<Tuple2<T1,T2>> (where you need to replace T1 and T2 with actual types) and serialize that property instead. In the Setter of that property, you should convert to Dictionary, and in the Getter, you should convert from Dictionary.

Alternatively, you can wait for us to implement the DataContractSerializer, which is expected in a few months.

Thank you.
Regards,
JS-Support