using System; using System.Collections; using HeuristicLab.Persistence.Core; using HeuristicLab.Persistence.Interfaces; using System.Collections.Generic; namespace HeuristicLab.Persistence.Default.Decomposers { public class DictionaryDecomposer : IDecomposer { public bool CanDecompose(Type type) { return type.GetInterface(typeof(IDictionary).FullName) != null; } 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) { return Activator.CreateInstance(t, true); } public object Populate(object instance, IEnumerable o, Type t) { IDictionary dict = (IDictionary)instance; IEnumerator iter = o.GetEnumerator(); while (iter.MoveNext()) { object key = iter.Current.Value; iter.MoveNext(); object value = iter.Current.Value; dict.Add(key, value); } return dict; } } }