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
RevLine 
[1454]1using System;
2using HeuristicLab.Persistence.Core;
[1703]3using HeuristicLab.Persistence.Auxiliary;
[1454]4using HeuristicLab.Persistence.Interfaces;
5using System.Collections.Generic;
[1823]6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1454]7
[1823]8namespace 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}
Note: See TracBrowser for help on using the repository browser.