Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Decomposers/TypeDecomposer.cs @ 1625

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

Added PersistenceException used consistently for all error conditions in the persistence framework (#548)

File size: 1.7 KB
Line 
1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
4using System.Collections.Generic;
5using HeuristicLab.Persistence.Default.Decomposers.Storable;
6
7namespace 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}
Note: See TracBrowser for help on using the repository browser.