Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3205 was 3036, checked in by epitzer, 14 years ago

make most serializers internal and complete API documentation (#548)

File size: 1.9 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  [StorableClass]
11  internal sealed 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}
Note: See TracBrowser for help on using the repository browser.