Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/KeyValuePairSerializer.cs @ 16571

Last change on this file since 16571 was 16571, checked in by gkronber, 5 years ago

#2520: several fixes based on failing unit tests

  • adapted unit tests that check StorableConstructors
  • missing translations of StorableClass -> StorableType
  • missing StorableConstructors
  • missing or unecessary PluginDependencies / ProjectReferences
File size: 3.5 KB
RevLine 
[3742]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3742]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;
[4068]23using System.Collections.Generic;
[1454]24using System.Linq;
[4068]25using System.Reflection;
[1454]26using HeuristicLab.Persistence.Core;
[16565]27using HEAL.Attic;
[1454]28using HeuristicLab.Persistence.Interfaces;
29
[1823]30namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1566]31
[16571]32  [StorableType("7A42AEE3-A9C5-46B7-9968-F8EF3E146DF3")]
[3036]33  internal sealed class KeyValuePairSerializer : ICompositeSerializer {
[1454]34
[4806]35    [StorableConstructor]
[16565]36    private KeyValuePairSerializer(StorableConstructorFlag _) { }
[4806]37    public KeyValuePairSerializer() { }
38
[1539]39    public int Priority {
40      get { return 100; }
41    }
42
[2993]43    private static readonly Type genericKeyValuePairType =
44      typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
[1539]45
[1823]46    public bool CanSerialize(Type type) {
[1454]47      return type.IsGenericType &&
[4068]48             type.GetGenericTypeDefinition() == genericKeyValuePairType;
[1454]49    }
50
[2993]51    public string JustifyRejection(Type type) {
52      if (!type.IsGenericType)
[4068]53        return "not even generic";
[2993]54      return "not generic KeyValuePair<,>";
55    }
56
[1553]57    public IEnumerable<Tag> CreateMetaInfo(object o) {
58      return new Tag[] { };
59    }
60
61    public IEnumerable<Tag> Decompose(object o) {
[1454]62      Type t = o.GetType();
[1625]63      Tag key, value;
64      try {
[1703]65        key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
[4068]66      }
67      catch (Exception e) {
[1625]68        throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
69      }
70      yield return key;
71      try {
72        value = new Tag("value", t.GetProperty("Value").GetValue(o, null));
[4068]73      }
74      catch (Exception e) {
[1625]75        throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
76      }
77      yield return value;
[1454]78    }
79
[1553]80    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
[1454]81      return Activator.CreateInstance(type, true);
82    }
83
[1553]84    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
[1454]85      IEnumerator<Tag> iter = o.GetEnumerator();
[1625]86      try {
87        iter.MoveNext();
88        t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
89          .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
90        iter.MoveNext();
91        t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
92          .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
[4068]93      }
94      catch (InvalidOperationException e) {
[1625]95        throw new PersistenceException("Not enough components to populate KeyValuePair instance", e);
[4068]96      }
97      catch (Exception e) {
[1625]98        throw new PersistenceException("Exception caught during KeyValuePair reconstruction", e);
99      }
[1553]100    }
[1454]101  }
102}
Note: See TracBrowser for help on using the repository browser.