[1454] | 1 | using System;
|
---|
| 2 | using System.Linq;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using HeuristicLab.Persistence.Core;
|
---|
| 5 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 6 | using System.Reflection;
|
---|
[1623] | 7 | using HeuristicLab.Persistence.Default.Decomposers.Storable;
|
---|
[1454] | 8 |
|
---|
| 9 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
[1566] | 10 |
|
---|
[1563] | 11 | [EmptyStorableClass]
|
---|
[1539] | 12 | public class KeyValuePairDecomposer : IDecomposer {
|
---|
[1454] | 13 |
|
---|
[1539] | 14 | public int Priority {
|
---|
| 15 | get { return 100; }
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 |
|
---|
[1454] | 19 | public bool CanDecompose(Type type) {
|
---|
| 20 | return type.IsGenericType &&
|
---|
[1566] | 21 | type.GetGenericTypeDefinition() ==
|
---|
| 22 | typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
|
---|
[1454] | 23 | }
|
---|
| 24 |
|
---|
[1553] | 25 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
| 26 | return new Tag[] { };
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public IEnumerable<Tag> Decompose(object o) {
|
---|
[1454] | 30 | Type t = o.GetType();
|
---|
[1625] | 31 | Tag key, value;
|
---|
| 32 | try {
|
---|
| 33 | key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
|
---|
| 34 | } catch (Exception e) {
|
---|
| 35 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
| 36 | }
|
---|
| 37 | yield return key;
|
---|
| 38 | try {
|
---|
| 39 | value = new Tag("value", t.GetProperty("Value").GetValue(o, null));
|
---|
| 40 | } catch (Exception e) {
|
---|
| 41 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
| 42 | }
|
---|
| 43 | yield return value;
|
---|
[1454] | 44 | }
|
---|
| 45 |
|
---|
[1553] | 46 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[1454] | 47 | return Activator.CreateInstance(type, true);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[1553] | 50 | public void Populate(object instance, IEnumerable<Tag> o, Type t) {
|
---|
[1454] | 51 | IEnumerator<Tag> iter = o.GetEnumerator();
|
---|
[1625] | 52 | try {
|
---|
| 53 | iter.MoveNext();
|
---|
| 54 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
| 55 | .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
|
---|
| 56 | iter.MoveNext();
|
---|
| 57 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
| 58 | .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
|
---|
| 59 | } catch (InvalidOperationException e) {
|
---|
| 60 | throw new PersistenceException("Not enough components to populate KeyValuePair instance", e);
|
---|
| 61 | } catch (Exception e) {
|
---|
| 62 | throw new PersistenceException("Exception caught during KeyValuePair reconstruction", e);
|
---|
| 63 | }
|
---|
[1553] | 64 | }
|
---|
[1454] | 65 | }
|
---|
| 66 | }
|
---|