1 | using System;
|
---|
2 | using HeuristicLab.Persistence.Core;
|
---|
3 | using HeuristicLab.Persistence.Auxiliary;
|
---|
4 | using HeuristicLab.Persistence.Interfaces;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Persistence.Default.CompositeSerializers {
|
---|
9 |
|
---|
10 | [StorableClass]
|
---|
11 | public class TypeSerializer : ICompositeSerializer {
|
---|
12 |
|
---|
13 | public int Priority {
|
---|
14 | get { return 100; }
|
---|
15 | }
|
---|
16 |
|
---|
17 | public bool CanSerialize(Type type) {
|
---|
18 | return type == typeof(Type) ||
|
---|
19 | type.VersionInvariantName() == "System.RuntimeType, mscorlib";
|
---|
20 | }
|
---|
21 |
|
---|
22 | public string JustifyRejection(Type type) {
|
---|
23 | return "not System.Type nor System.RuntimeType";
|
---|
24 | }
|
---|
25 |
|
---|
26 | public IEnumerable<Tag> CreateMetaInfo(object o) {
|
---|
27 | yield return new Tag("AssemblyQualifiedName", ((Type)o).AssemblyQualifiedName);
|
---|
28 | }
|
---|
29 |
|
---|
30 | public IEnumerable<Tag> Decompose(object obj) {
|
---|
31 | return new Tag[] { };
|
---|
32 | }
|
---|
33 |
|
---|
34 | public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
|
---|
35 | IEnumerator<Tag> it = metaInfo.GetEnumerator();
|
---|
36 | try {
|
---|
37 | it.MoveNext();
|
---|
38 | } catch (InvalidOperationException e) {
|
---|
39 | throw new PersistenceException("Insufficient meta information to instantiate Type object", e);
|
---|
40 | }
|
---|
41 | try {
|
---|
42 | return TypeLoader.Load((string)it.Current.Value);
|
---|
43 | } catch (InvalidCastException e) {
|
---|
44 | throw new PersistenceException("Invalid meta information during reconstruction of Type object", e);
|
---|
45 | } catch (TypeLoadException e) {
|
---|
46 | throw new PersistenceException(String.Format(
|
---|
47 | "Cannot load Type {0}, make sure all required assemblies are available.",
|
---|
48 | (string)it.Current.Value), e);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
|
---|
53 | // Type ojects are populated during instance creation.
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|