1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Reflection;
|
---|
26 | using HeuristicLab.Persistence.Auxiliary;
|
---|
27 | using HeuristicLab.Persistence.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Persistence.Interfaces;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
32 |
|
---|
33 | [StorableClass]
|
---|
34 | internal sealed class EnumerableSerializer : ICompositeSerializer {
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | private EnumerableSerializer(bool deserializing) { }
|
---|
38 | public EnumerableSerializer() { }
|
---|
39 |
|
---|
40 | public int Priority {
|
---|
41 | get { return 100; }
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | public bool CanSerialize(Type type) {
|
---|
46 | return
|
---|
47 | ReflectionTools.HasDefaultConstructor(type) &&
|
---|
48 | type.GetInterface(typeof(IEnumerable).FullName) != null &&
|
---|
49 | type.GetMethod("Add") != null &&
|
---|
50 | type.GetMethod("Add").GetParameters().Length == 1;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public string JustifyRejection(Type type) {
|
---|
54 | if (!ReflectionTools.HasDefaultConstructor(type))
|
---|
55 | return "no default constructor";
|
---|
56 | if (type.GetInterface(typeof(IEnumerable).FullName) == null)
|
---|
57 | return "interface IEnumerable not implemented";
|
---|
58 | if (type.GetMethod("Add") == null)
|
---|
59 | return "no 'Add()' method";
|
---|
60 | return "no 'Add()' method with one argument";
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
64 | return new Tag[] { };
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
68 | foreach (object o in (IEnumerable)obj) {
|
---|
69 | yield return new Tag(o);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
74 | return Activator.CreateInstance(type, true);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void Populate(object instance, IEnumerable<Tag> tags, Type type) {
|
---|
78 | MethodInfo addMethod = type.GetMethod("Add");
|
---|
79 | try {
|
---|
80 | foreach (var tag in tags)
|
---|
81 | addMethod.Invoke(instance, new[] { tag.Value });
|
---|
82 | }
|
---|
83 | catch (Exception e) {
|
---|
84 | throw new PersistenceException("Exception caught while trying to populate enumerable.", e);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | } |
---|