Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/NumberEnumerable2StringDecomposer.cs @ 1728

Last change on this file since 1728 was 1705, checked in by epitzer, 16 years ago

Check for default constructor in all decomposers to ensure failure during serialization instead of deserialization. (#606)

File size: 4.1 KB
Line 
1using System;
2using HeuristicLab.Persistence.Interfaces;
3using HeuristicLab.Persistence.Core;
4using System.Collections.Generic;
5using System.Reflection;
6using System.Globalization;
7using System.Text;
8using HeuristicLab.Persistence.Default.Decomposers.Storable;
9using HeuristicLab.Persistence.Auxiliary;
10
11namespace HeuristicLab.Persistence.Default.Decomposers {
12
13  [EmptyStorableClass]
14  public class NumberEnumerable2StringDecomposer : IDecomposer {
15
16    public int Priority {
17      get { return 200; }
18    }
19
20    private static readonly Number2StringDecomposer numberConverter =
21      new Number2StringDecomposer();
22
23    private static readonly Dictionary<Type, Type> interfaceCache = new Dictionary<Type, Type>();
24
25    public Type GetGenericEnumerableInterface(Type type) {
26      if (interfaceCache.ContainsKey(type))
27        return interfaceCache[type];
28      foreach (Type iface in type.GetInterfaces()) {
29        if (iface.IsGenericType &&
30          iface.GetGenericTypeDefinition() == typeof(IEnumerable<>) &&
31          numberConverter.CanDecompose(iface.GetGenericArguments()[0])) {
32          interfaceCache.Add(type, iface);
33          return iface;
34        }
35      }
36      interfaceCache.Add(type, null);
37      return null;
38    }
39
40    public bool ImplementsGenericEnumerable(Type type) {
41      return GetGenericEnumerableInterface(type) != null;
42    }
43
44    public bool HasAddMethod(Type type) {
45      return
46        type.GetMethod("Add") != null &&
47        type.GetMethod("Add").GetParameters().Length == 1 &&
48        type.GetConstructor(
49          BindingFlags.Public |
50          BindingFlags.NonPublic |
51          BindingFlags.Instance,
52          null, Type.EmptyTypes, null) != null;
53    }
54
55    public bool CanDecompose(Type type) {
56      return
57        ReflectionTools.HasDefaultConstructor(type) &&
58        ImplementsGenericEnumerable(type) &&
59        HasAddMethod(type);
60    }
61
62    public IEnumerable<Tag> CreateMetaInfo(object o) {
63      return new Tag[] { };
64    }
65
66    public IEnumerable<Tag> Decompose(object obj) {
67      Type type = obj.GetType();
68      Type enumerable = GetGenericEnumerableInterface(type);
69      InterfaceMapping iMap = obj.GetType().GetInterfaceMap(enumerable);
70      MethodInfo getEnumeratorMethod =
71        iMap.TargetMethods[
72        Array.IndexOf(
73          iMap.InterfaceMethods,
74          enumerable.GetMethod("GetEnumerator"))];
75      object[] empty = new object[] { };
76      object genericEnumerator = getEnumeratorMethod.Invoke(obj, empty);
77      MethodInfo moveNextMethod = genericEnumerator.GetType().GetMethod("MoveNext");
78      PropertyInfo currentProperty = genericEnumerator.GetType().GetProperty("Current");
79      StringBuilder sb = new StringBuilder();
80      while ((bool)moveNextMethod.Invoke(genericEnumerator, empty))
81        sb.Append(
82          numberConverter.Format(
83            currentProperty.GetValue(genericEnumerator, null))).Append(';');
84      yield return new Tag("compact enumerable", sb.ToString());
85    }
86
87    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
88      return Activator.CreateInstance(type, true);
89    }
90
91    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
92      Type enumerable = GetGenericEnumerableInterface(type);
93      Type elementType = enumerable.GetGenericArguments()[0];
94      MethodInfo addMethod = type.GetMethod("Add");
95      try {
96        var tagEnumerator = tags.GetEnumerator();
97        tagEnumerator.MoveNext();
98        string[] stringValues = ((string)tagEnumerator.Current.Value)
99          .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
100        foreach (var value in stringValues) {
101          addMethod.Invoke(instance, new[] { numberConverter.Parse(value, elementType) });
102        }
103      } catch (InvalidOperationException e) {
104        throw new PersistenceException("Insufficient element data to reconstruct number enumerable", e);
105      } catch (InvalidCastException e) {
106        throw new PersistenceException("Invalid element data during reconstruction of number enumerable", e);
107      }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.