Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TypeSerializer.cs @ 1960

Last change on this file since 1960 was 1843, checked in by epitzer, 15 years ago

Update TypeSerializer use full AssemblyQualifiedName when serializing Type objects. (#603)

File size: 1.8 KB
Line 
1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Auxiliary;
4using HeuristicLab.Persistence.Interfaces;
5using System.Collections.Generic;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Persistence.Default.CompositeSerializers {
9
10  [EmptyStorableClass]
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 IEnumerable<Tag> CreateMetaInfo(object o) {
23      yield return new Tag("AssemblyQualifiedName", ((Type)o).AssemblyQualifiedName);
24    }
25
26    public IEnumerable<Tag> Decompose(object obj) {
27      return new Tag[] { };
28    }
29
30    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
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);
36      }
37      try {
38        return TypeLoader.Load((string)it.Current.Value);
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      }
46    }
47
48    public void Populate(object instance, IEnumerable<Tag> objects, Type type) {
49      // Type ojects are populated during instance creation.
50    }
51  }
52}
Note: See TracBrowser for help on using the repository browser.