I am using the SerializableDictionary
defined in this blog entry to store <string, object>
data and pass it to/from a WCF service. This works fine if I use value types as the values, because they can be boxed easily to become object
s. However, if I use something like
new List<int>() { 5, 10 }
, I get an Exception:
The type System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context.
According to the discussion here, I should be using value.GetType()
to initialize the XmlSerializer
; however, while that lets me serialize, I don't know how to deserialize in a general way back to my SerializableDictionary
.
I'm not sure if there's a way to change this cleanly while still allowing <string, object>
as my type arguments - I can serialize the value to binary instead of XML and transport it that way (the same code is serializing and deserializing, so I'm not concerned about interoperability), but I would like to have the XML if at all possible.
EDIT
Full code example:
XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, object>));
SerializableDictionary<string, object> dic = new SerializableDictionary<string, object>();
dic["test"] = new List<int>() { 5, 10 };
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
serializer.Serialize(writer, dic);
string ser = sb.ToString();
SOLUTION
Thanks to Nico Schertler for giving me the right answer. I'm posting my final code here in case anyone needs it. This is backward-compatible with the original code in the first link, so anything that was serialized by that code can be deserialized by the below.
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
#region " IXmlSerializable Members "
#region " WriteXml "
public void WriteXml(XmlWriter writer)
{
// Base types
string baseKeyType = typeof(TKey).AssemblyQualifiedName;
string baseValueType = typeof(TValue).AssemblyQualifiedName;
writer.WriteAttributeString("keyType", baseKeyType);
writer.WriteAttributeString("valueType", baseValueType);
foreach (TKey key in this.Keys)
{
// Start
writer.WriteStartElement("item");
// Key
Type keyType = key.GetType();
XmlSerializer keySerializer = GetTypeSerializer(keyType.AssemblyQualifiedName);
writer.WriteStartElement("key");
if (keyType != typeof(TKey)) { writer.WriteAttributeString("type", keyType.AssemblyQualifiedName); }
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
// Value
TValue value = this[key];
Type valueType = value.GetType();
XmlSerializer valueSerializer = GetTypeSerializer(valueType.AssemblyQualifiedName);
writer.WriteStartElement("value");
if (valueType != typeof(TValue)) { writer.WriteAttributeString("type", valueType.AssemblyQualifiedName); }
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
// End
writer.WriteEndElement();
}
}
#endregion
#region " ReadXml "
public void ReadXml(XmlReader reader)
{
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
{
return;
}
// Base types
string baseKeyType = typeof(TKey).AssemblyQualifiedName;
string baseValueType = typeof(TValue).AssemblyQualifiedName;
while (reader.NodeType != XmlNodeType.EndElement)
{
// Start
reader.ReadStartElement("item");
// Key
XmlSerializer keySerializer = GetTypeSerializer(reader["type"] ?? baseKeyType);
reader.ReadStartElement("key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
// Value
XmlSerializer valueSerializer = GetTypeSerializer(reader["type"] ?? baseValueType);
reader.ReadStartElement("value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
// Store
this.Add(key, value);
// End
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
#endregion
#region " GetSchema "
public XmlSchema GetSchema()
{
return null;
}
#endregion
#endregion
#region " GetTypeSerializer "
private static readonly Dictionary<string, XmlSerializer> _serializers = new Dictionary<string, XmlSerializer>();
private static readonly object _deadbolt = new object();
private XmlSerializer GetTypeSerializer(string type)
{
if (!_serializers.ContainsKey(type))
{
lock (_deadbolt)
{
if (!_serializers.ContainsKey(type))
{
_serializers.Add(type, new XmlSerializer(Type.GetType(type)));
}
}
}
return _serializers[type];
}
#endregion
}
I'm only writing out the type if it's different than the base type in order to keep the length of the XML down, and I'm keeping a static list of XmlSerializer
s in order to prevent intantiating them all over the place. I did have to write out the provided types at the beginning in order to be able to prevent writing out the type on each node.