Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3199 was 3036, checked in by epitzer, 15 years ago

make most serializers internal and complete API documentation (#548)

File size: 2.6 KB
RevLine 
[1454]1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Persistence.Core;
5using HeuristicLab.Persistence.Interfaces;
6using System.Reflection;
[1823]7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1454]8
[1823]9namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1566]10
[3017]11  [StorableClass]
[3036]12  internal sealed class KeyValuePairSerializer : ICompositeSerializer {
[1454]13
[1539]14    public int Priority {
15      get { return 100; }
16    }
17
[2993]18    private static readonly Type genericKeyValuePairType =
19      typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
[1539]20
[1823]21    public bool CanSerialize(Type type) {
[1454]22      return type.IsGenericType &&
[2993]23             type.GetGenericTypeDefinition() == genericKeyValuePairType;             
[1454]24    }
25
[2993]26    public string JustifyRejection(Type type) {
27      if (!type.IsGenericType)
28        return "not even generic";     
29      return "not generic KeyValuePair<,>";
30    }
31
[1553]32    public IEnumerable<Tag> CreateMetaInfo(object o) {
33      return new Tag[] { };
34    }
35
36    public IEnumerable<Tag> Decompose(object o) {
[1454]37      Type t = o.GetType();
[1625]38      Tag key, value;
39      try {
[1703]40        key = new Tag("key", t.GetProperty("Key").GetValue(o, null));
[1625]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;
[1454]51    }
52
[1553]53    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
[1454]54      return Activator.CreateInstance(type, true);
55    }
56
[1553]57    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
[1454]58      IEnumerator<Tag> iter = o.GetEnumerator();
[1625]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      }
[1553]71    }
[1454]72  }
73}
Note: See TracBrowser for help on using the repository browser.