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