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 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
10 |
|
---|
11 | [StorableClass]
|
---|
12 | public class KeyValuePairSerializer : ICompositeSerializer {
|
---|
13 |
|
---|
14 | public int Priority {
|
---|
15 | get { return 100; }
|
---|
16 | }
|
---|
17 |
|
---|
18 | private static readonly Type genericKeyValuePairType =
|
---|
19 | typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
|
---|
20 |
|
---|
21 | public bool CanSerialize(Type type) {
|
---|
22 | return type.IsGenericType &&
|
---|
23 | type.GetGenericTypeDefinition() == genericKeyValuePairType;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public string JustifyRejection(Type type) {
|
---|
27 | if (!type.IsGenericType)
|
---|
28 | return "not even generic";
|
---|
29 | return "not generic KeyValuePair<,>";
|
---|
30 | }
|
---|
31 |
|
---|
32 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
33 | return new Tag[] { };
|
---|
34 | }
|
---|
35 |
|
---|
36 | public IEnumerable<Tag> Decompose(object o) {
|
---|
37 | Type t = o.GetType();
|
---|
38 | Tag key, value;
|
---|
39 | try {
|
---|
40 | key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
|
---|
41 | } catch (Exception e) {
|
---|
42 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
43 | }
|
---|
44 | yield return key;
|
---|
45 | try {
|
---|
46 | value = new Tag("value", t.GetProperty("Value").GetValue(o, null));
|
---|
47 | } catch (Exception e) {
|
---|
48 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
49 | }
|
---|
50 | yield return value;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
54 | return Activator.CreateInstance(type, true);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void Populate(object instance, IEnumerable<Tag> o, Type t) {
|
---|
58 | IEnumerator<Tag> iter = o.GetEnumerator();
|
---|
59 | try {
|
---|
60 | iter.MoveNext();
|
---|
61 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
62 | .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
|
---|
63 | iter.MoveNext();
|
---|
64 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
65 | .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
|
---|
66 | } catch (InvalidOperationException e) {
|
---|
67 | throw new PersistenceException("Not enough components to populate KeyValuePair instance", e);
|
---|
68 | } catch (Exception e) {
|
---|
69 | throw new PersistenceException("Exception caught during KeyValuePair reconstruction", e);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|