1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using HeuristicLab.Persistence.Core;
|
---|
26 | using HeuristicLab.Persistence.Interfaces;
|
---|
27 | using System.Reflection;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
31 |
|
---|
32 | [StorableClass]
|
---|
33 | internal sealed class KeyValuePairSerializer : ICompositeSerializer {
|
---|
34 |
|
---|
35 | public int Priority {
|
---|
36 | get { return 100; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private static readonly Type genericKeyValuePairType =
|
---|
40 | typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
|
---|
41 |
|
---|
42 | public bool CanSerialize(Type type) {
|
---|
43 | return type.IsGenericType &&
|
---|
44 | type.GetGenericTypeDefinition() == genericKeyValuePairType;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public string JustifyRejection(Type type) {
|
---|
48 | if (!type.IsGenericType)
|
---|
49 | return "not even generic";
|
---|
50 | return "not generic KeyValuePair<,>";
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
54 | return new Tag[] { };
|
---|
55 | }
|
---|
56 |
|
---|
57 | public IEnumerable<Tag> Decompose(object o) {
|
---|
58 | Type t = o.GetType();
|
---|
59 | Tag key, value;
|
---|
60 | try {
|
---|
61 | key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
|
---|
62 | } catch (Exception e) {
|
---|
63 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
64 | }
|
---|
65 | yield return key;
|
---|
66 | try {
|
---|
67 | value = new Tag("value", t.GetProperty("Value").GetValue(o, null));
|
---|
68 | } catch (Exception e) {
|
---|
69 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
70 | }
|
---|
71 | yield return value;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
75 | return Activator.CreateInstance(type, true);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void Populate(object instance, IEnumerable<Tag> o, Type t) {
|
---|
79 | IEnumerator<Tag> iter = o.GetEnumerator();
|
---|
80 | try {
|
---|
81 | iter.MoveNext();
|
---|
82 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
83 | .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
|
---|
84 | iter.MoveNext();
|
---|
85 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
86 | .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
|
---|
87 | } catch (InvalidOperationException e) {
|
---|
88 | throw new PersistenceException("Not enough components to populate KeyValuePair instance", e);
|
---|
89 | } catch (Exception e) {
|
---|
90 | throw new PersistenceException("Exception caught during KeyValuePair reconstruction", e);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|