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