Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/KeyValuePairSerializer.cs @ 14927

Last change on this file since 14927 was 14927, checked in by gkronber, 7 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using HeuristicLab.Persistence.Core;
27using HeuristicLab.Persistence;
28using HeuristicLab.Persistence.Interfaces;
29
30namespace HeuristicLab.Persistence.Default.CompositeSerializers {
31
32  [StorableType("97d00efc-4eb0-4dc7-a58c-977c45c2904c")]
33  internal sealed class KeyValuePairSerializer : ICompositeSerializer {
34
35    [StorableConstructor]
36    private KeyValuePairSerializer(bool deserializing) { }
37    public KeyValuePairSerializer() { }
38
39    public int Priority {
40      get { return 100; }
41    }
42
43    private static readonly Type genericKeyValuePairType =
44      typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
45
46    public bool CanSerialize(Type type) {
47      return type.IsGenericType &&
48             type.GetGenericTypeDefinition() == genericKeyValuePairType;
49    }
50
51    public string JustifyRejection(Type type) {
52      if (!type.IsGenericType)
53        return "not even generic";
54      return "not generic KeyValuePair<,>";
55    }
56
57    public IEnumerable<Tag> CreateMetaInfo(object o) {
58      return new Tag[] { };
59    }
60
61    public IEnumerable<Tag> Decompose(object o) {
62      Type t = o.GetType();
63      Tag key, value;
64      try {
65        key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
66      } catch (Exception e) {
67        throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
68      }
69      yield return key;
70      try {
71        value = new Tag("value", t.GetProperty("Value").GetValue(o, null));
72      } catch (Exception e) {
73        throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
74      }
75      yield return value;
76    }
77
78    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
79      return Activator.CreateInstance(type, true);
80    }
81
82    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
83      IEnumerator<Tag> iter = o.GetEnumerator();
84      try {
85        iter.MoveNext();
86        t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
87          .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
88        iter.MoveNext();
89        t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
90          .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
91      } catch (InvalidOperationException e) {
92        throw new PersistenceException("Not enough components to populate KeyValuePair instance", e);
93      } catch (Exception e) {
94        throw new PersistenceException("Exception caught during KeyValuePair reconstruction", e);
95      }
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.