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;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Persistence.Default.Decomposers {
|
---|
9 |
|
---|
10 | [EmptyStorableClass]
|
---|
11 | public class KeyValuePairDecomposer : IDecomposer {
|
---|
12 |
|
---|
13 | public int Priority {
|
---|
14 | get { return 100; }
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | public bool CanDecompose(Type type) {
|
---|
19 | return type.IsGenericType &&
|
---|
20 | type.GetGenericTypeDefinition() ==
|
---|
21 | typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
|
---|
22 | }
|
---|
23 |
|
---|
24 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
25 | return new Tag[] { };
|
---|
26 | }
|
---|
27 |
|
---|
28 | public IEnumerable<Tag> Decompose(object o) {
|
---|
29 | Type t = o.GetType();
|
---|
30 | yield return new Tag("key", t.GetProperty("Key").GetValue(o, null));
|
---|
31 | yield return new Tag("value", t.GetProperty("Value").GetValue(o, null));
|
---|
32 | }
|
---|
33 |
|
---|
34 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
35 | return Activator.CreateInstance(type, true);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void Populate(object instance, IEnumerable<Tag> o, Type t) {
|
---|
39 | IEnumerator<Tag> iter = o.GetEnumerator();
|
---|
40 | iter.MoveNext();
|
---|
41 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
42 | .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
|
---|
43 | iter.MoveNext();
|
---|
44 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
45 | .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|