1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 HeuristicLab.Persistence.Core;
|
---|
24 | using HeuristicLab.Persistence.Auxiliary;
|
---|
25 | using HeuristicLab.Persistence.Interfaces;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
30 |
|
---|
31 | [StorableClass]
|
---|
32 | internal sealed class TypeSerializer : ICompositeSerializer {
|
---|
33 |
|
---|
34 | public int Priority {
|
---|
35 | get { return 100; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public bool CanSerialize(Type type) {
|
---|
39 | return type == typeof(Type) ||
|
---|
40 | type.VersionInvariantName() == "System.RuntimeType, mscorlib";
|
---|
41 | }
|
---|
42 |
|
---|
43 | public string JustifyRejection(Type type) {
|
---|
44 | return "not System.Type nor System.RuntimeType";
|
---|
45 | }
|
---|
46 |
|
---|
47 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
48 | yield return new Tag("AssemblyQualifiedName", ((Type)o).AssemblyQualifiedName);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
52 | return new Tag[] { };
|
---|
53 | }
|
---|
54 |
|
---|
55 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
56 | IEnumerator<Tag> it = metaInfo.GetEnumerator();
|
---|
57 | try {
|
---|
58 | it.MoveNext();
|
---|
59 | } catch (InvalidOperationException e) {
|
---|
60 | throw new PersistenceException("Insufficient meta information to instantiate Type object", e);
|
---|
61 | }
|
---|
62 | try {
|
---|
63 | return TypeLoader.Load((string)it.Current.Value);
|
---|
64 | } catch (InvalidCastException e) {
|
---|
65 | throw new PersistenceException("Invalid meta information during reconstruction of Type object", e);
|
---|
66 | } catch (TypeLoadException e) {
|
---|
67 | throw new PersistenceException(String.Format(
|
---|
68 | "Cannot load Type {0}, make sure all required assemblies are available.",
|
---|
69 | (string)it.Current.Value), e);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
|
---|
74 | // Type ojects are populated during instance creation.
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|