Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TupleSerializer.cs @ 16755

Last change on this file since 16755 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Default.CompositeSerializers.Storable;
28using HeuristicLab.Persistence.Interfaces;
29
30namespace HeuristicLab.Persistence.Default.CompositeSerializers {
31
32  [StorableClass]
33  internal sealed class TupleSerializer : ICompositeSerializer {
34
35    [StorableConstructor]
36    private TupleSerializer(bool deserializing) { }
37    public TupleSerializer() { }
38
39    public int Priority {
40      get { return 50; }
41    }
42
43    public bool CanSerialize(Type type) {
44      if (!type.IsGenericType)
45        return false;
46      Type t = type.GetGenericTypeDefinition();
47      return t == typeof(Tuple<>) ||
48        t == typeof(Tuple<,>) ||
49        t == typeof(Tuple<,,>) ||
50        t == typeof(Tuple<,,,>) ||
51        t == typeof(Tuple<,,,,>) ||
52        t == typeof(Tuple<,,,,,>) ||
53        t == typeof(Tuple<,,,,,,>) ||
54        t == typeof(Tuple<,,,,,,,>);
55    }
56
57    public string JustifyRejection(Type type) {
58      if (!type.IsGenericType)
59        return "not a generic type";
60      return "type is not a tuple type";
61    }
62
63    public IEnumerable<Tag> CreateMetaInfo(object obj) {
64      Type t = obj.GetType();
65      for (int i = 1; i <= t.GetGenericArguments().Length; i++) {
66        string name = string.Format("Item{0}", i);
67        yield return new Tag(name, t.GetProperty(name).GetValue(obj, null));
68      }
69    }
70
71    public IEnumerable<Tag> Decompose(object obj) {
72      return null;
73    }
74
75    private static BindingFlags public_static = BindingFlags.Public | BindingFlags.Static;
76
77    private static MethodInfo[] CreateMethods = new MethodInfo[8];
78
79    static TupleSerializer() {
80      foreach (MethodInfo mi in typeof(Tuple).GetMethods(public_static).Where(mi => mi.Name == "Create")) {
81        CreateMethods[mi.GetGenericArguments().Length - 1] = mi;
82      }
83    }
84
85    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
86      var values = metaInfo.Select(t => t.Value).ToArray();
87
88      MethodInfo createMethod = CreateMethods[values.Length - 1].MakeGenericMethod(type.GetGenericArguments());
89      return createMethod.Invoke(null, values);
90    }
91
92    public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
93      // nothing to do
94    }
95
96  }
97}
Note: See TracBrowser for help on using the repository browser.