[5698] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5698] | 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Reflection;
|
---|
| 26 | using HeuristicLab.Persistence.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Persistence.Interfaces;
|
---|
| 29 |
|
---|
| 30 | namespace 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();
|
---|
[5714] | 65 | for (int i = 1; i <= t.GetGenericArguments().Length; i++) {
|
---|
[5698] | 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")) {
|
---|
[5714] | 81 | CreateMethods[mi.GetGenericArguments().Length - 1] = mi;
|
---|
[5698] | 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
| 86 | var values = metaInfo.Select(t => t.Value).ToArray();
|
---|
[5809] | 87 |
|
---|
[5714] | 88 | MethodInfo createMethod = CreateMethods[values.Length - 1].MakeGenericMethod(type.GetGenericArguments());
|
---|
| 89 | return createMethod.Invoke(null, values);
|
---|
[5698] | 90 | }
|
---|
| 91 |
|
---|
| 92 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
| 93 | // nothing to do
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | }
|
---|
| 97 | }
|
---|