Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeLoader.cs @ 3004

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

add complete persistence API docs (#548)

File size: 2.1 KB
Line 
1using System.Collections.Generic;
2using System;
3using HeuristicLab.Persistence.Interfaces;
4using HeuristicLab.Persistence.Core.Tokens;
5using HeuristicLab.Persistence.Auxiliary;
6using HeuristicLab.Tracing;
7using System.Reflection;
8using HeuristicLab.Persistence.Core;
9
10namespace HeuristicLab.Persistence.Auxiliary {
11
12  internal class TypeLoader {
13
14    public static Type Load(string typeNameString) {
15      Type type;
16      try {
17        type = Type.GetType(typeNameString, true);
18      } catch (Exception) {
19        Logger.Warn(String.Format(
20          "Cannot load type \"{0}\", falling back to partial name", typeNameString));
21        try {
22          TypeName typeName = TypeNameParser.Parse(typeNameString);
23          Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);
24          type = a.GetType(typeName.ToString(false, false), true);
25        } catch (Exception) {
26          throw new PersistenceException(String.Format(
27            "Could not load type \"{0}\"",
28            typeNameString));
29        }
30        try {
31          TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
32          TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
33          if (!requestedTypeName.IsCompatible(loadedTypeName))
34            throw new PersistenceException(String.Format(
35              "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}",
36              typeNameString,
37              type.AssemblyQualifiedName));
38          if (requestedTypeName.IsNewerThan(loadedTypeName))
39            throw new PersistenceException(String.Format(
40              "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
41              typeNameString,
42              type.AssemblyQualifiedName));
43        } catch (PersistenceException) {
44          throw;
45        } catch (Exception e) {
46          Logger.Warn(String.Format(
47            "Could not perform version check requested type was {0} while loaded type is {1}:",
48            typeNameString,
49            type.AssemblyQualifiedName),
50            e);
51        }
52      }
53      return type;
54    }
55  }
56}
Note: See TracBrowser for help on using the repository browser.