I'm investigating about XML serialization, and since I use lot of dictionary, I would like to serialize them as well. I found the following solution for that (I'm quite proud of it! :) ).
[XmlInclude(typeof(Foo))]
public class XmlDictionary<TKey, TValue>
{
/// <summary>
/// Key/value pair.
/// </summary>
public struct DictionaryItem
{
/// <summary>
/// Dictionary item key.
/// </summary>
public TKey Key;
/// <summary>
/// Dictionary item value.
/// </summary>
public TValue Value;
}
/// <summary>
/// Dictionary items.
/// </summary>
public DictionaryItem[] Items
{
get {
List<DictionaryItem> items = new List<DictionaryItem>(ItemsDictionary.Count);
foreach (KeyValuePair<TKey, TValue> pair in ItemsDictionary) {
DictionaryItem item;
item.Key = pair.Key;
item.Value = pair.Value;
items.Add(item);
}
return (items.ToArray());
}
set {
ItemsDictionary = new Dictionary<TKey,TValue>();
foreach (DictionaryItem item in value)
ItemsDictionary.Add(item.Key, item.Value);
}
}
/// <summary>
/// Indexer base on dictionary key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public TValue this[TKey key]
{
get {
return (ItemsDictionary[key]);
}
set {
Debug.Assert(value != null);
ItemsDictionary[key] = value;
}
}
/// <summary>
/// Delegate for get key from a dictionary value.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public delegate TKey GetItemKeyDelegate(TValue value);
/// <summary>
/// Add a range of values automatically determining the associated keys.
/// </summary>
/// <param name="values"></param>
/// <param name="keygen"></param>
public void AddRange(IEnumerable<TValue> values, GetItemKeyDelegate keygen)
{
foreach (TValue v in values)
ItemsDictionary.Add(keygen(v), v);
}
/// <summary>
/// Items dictionary.
/// </summary>
[XmlIgnore]
public Dictionary<TKey, TValue> ItemsDictionary = new Dictionary<TKey,TValue>();
}
The classes deriving from this class are serialized in the following way:
<XmlProcessList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<DictionaryItemOfInt32Foo>
<Key/>
<Value/>
</DictionaryItemOfInt32XmlProcess>
<Items>
This give me a good solution, but:
How can I control the name of the element DictionaryItemOfInt32Foo
What happens if I define a Dictionary<FooInt32, Int32> and I have the classes Foo and FooInt32?
Is it possible to optimize the class above?
THank you very much!