[3742] | 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[1454] | 24 | using System.Linq;
|
---|
[4068] | 25 | using System.Reflection;
|
---|
[1454] | 26 | using HeuristicLab.Persistence.Core;
|
---|
[4068] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[1454] | 28 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 29 |
|
---|
[1823] | 30 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
[1566] | 31 |
|
---|
[3017] | 32 | [StorableClass]
|
---|
[3036] | 33 | internal sealed class KeyValuePairSerializer : ICompositeSerializer {
|
---|
[1454] | 34 |
|
---|
[1539] | 35 | public int Priority {
|
---|
| 36 | get { return 100; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[2993] | 39 | private static readonly Type genericKeyValuePairType =
|
---|
| 40 | typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
|
---|
[1539] | 41 |
|
---|
[1823] | 42 | public bool CanSerialize(Type type) {
|
---|
[1454] | 43 | return type.IsGenericType &&
|
---|
[4068] | 44 | type.GetGenericTypeDefinition() == genericKeyValuePairType;
|
---|
[1454] | 45 | }
|
---|
| 46 |
|
---|
[2993] | 47 | public string JustifyRejection(Type type) {
|
---|
| 48 | if (!type.IsGenericType)
|
---|
[4068] | 49 | return "not even generic";
|
---|
[2993] | 50 | return "not generic KeyValuePair<,>";
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[1553] | 53 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
| 54 | return new Tag[] { };
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public IEnumerable<Tag> Decompose(object o) {
|
---|
[1454] | 58 | Type t = o.GetType();
|
---|
[1625] | 59 | Tag key, value;
|
---|
| 60 | try {
|
---|
[1703] | 61 | key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
|
---|
[4068] | 62 | }
|
---|
| 63 | catch (Exception e) {
|
---|
[1625] | 64 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
| 65 | }
|
---|
| 66 | yield return key;
|
---|
| 67 | try {
|
---|
| 68 | value = new Tag("value", t.GetProperty("Value").GetValue(o, null));
|
---|
[4068] | 69 | }
|
---|
| 70 | catch (Exception e) {
|
---|
[1625] | 71 | throw new PersistenceException("Exception caught during KeyValuePair decomposition", e);
|
---|
| 72 | }
|
---|
| 73 | yield return value;
|
---|
[1454] | 74 | }
|
---|
| 75 |
|
---|
[1553] | 76 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
[1454] | 77 | return Activator.CreateInstance(type, true);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[1553] | 80 | public void Populate(object instance, IEnumerable<Tag> o, Type t) {
|
---|
[1454] | 81 | IEnumerator<Tag> iter = o.GetEnumerator();
|
---|
[1625] | 82 | try {
|
---|
| 83 | iter.MoveNext();
|
---|
| 84 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
| 85 | .Single(fi => fi.Name == "key").SetValue(instance, iter.Current.Value);
|
---|
| 86 | iter.MoveNext();
|
---|
| 87 | t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
|
---|
| 88 | .Single(fi => fi.Name == "value").SetValue(instance, iter.Current.Value);
|
---|
[4068] | 89 | }
|
---|
| 90 | catch (InvalidOperationException e) {
|
---|
[1625] | 91 | throw new PersistenceException("Not enough components to populate KeyValuePair instance", e);
|
---|
[4068] | 92 | }
|
---|
| 93 | catch (Exception e) {
|
---|
[1625] | 94 | throw new PersistenceException("Exception caught during KeyValuePair reconstruction", e);
|
---|
| 95 | }
|
---|
[1553] | 96 | }
|
---|
[1454] | 97 | }
|
---|
| 98 | }
|
---|