using System; using System.Collections; using HeuristicLab.Persistence.Core; using HeuristicLab.Persistence.Interfaces; using System.Collections.Generic; using HeuristicLab.Persistence.Default.Decomposers.Storable; using HeuristicLab.Persistence.Auxiliary; namespace HeuristicLab.Persistence.Default.Decomposers { [EmptyStorableClass] public class DictionaryDecomposer : IDecomposer { public int Priority { get { return 100; } } public bool CanDecompose(Type type) { return ReflectionTools.HasDefaultConstructor(type) && type.GetInterface(typeof(IDictionary).FullName) != null; } public IEnumerable CreateMetaInfo(object o) { return new Tag[] { }; } public IEnumerable Decompose(object o) { IDictionary dict = (IDictionary)o; foreach (DictionaryEntry entry in dict) { yield return new Tag("key", entry.Key); yield return new Tag("value", entry.Value); } } public object CreateInstance(Type t, IEnumerable metaInfo) { return Activator.CreateInstance(t, true); } public void Populate(object instance, IEnumerable o, Type t) { IDictionary dict = (IDictionary)instance; IEnumerator iter = o.GetEnumerator(); try { while (iter.MoveNext()) { Tag key = iter.Current; iter.MoveNext(); Tag value = iter.Current; dict.Add(key.Value, value.Value); } } catch (InvalidOperationException e) { throw new PersistenceException("Dictionaries must contain an even number of elements (key+value).", e); } catch (NotSupportedException e) { throw new PersistenceException("The serialized dictionary type was read-only or had a fixed size and cannot be deserialized.", e); } catch (ArgumentNullException e) { throw new PersistenceException("Dictionary key was null.", e); } catch (ArgumentException e) { throw new PersistenceException("Duplicate dictionary key.", e); } } } }