Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 2.3 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#pragma warning disable 0618
24          Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);         
25          // the suggested Assembly.Load() method fails to load assemblies outside the GAC
26#pragma warning restore 0618
27          type = a.GetType(typeName.ToString(false, false), true);
28        } catch (Exception) {
29          throw new PersistenceException(String.Format(
30            "Could not load type \"{0}\"",
31            typeNameString));
32        }
33        try {
34          TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
35          TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
36          if (!requestedTypeName.IsCompatible(loadedTypeName))
37            throw new PersistenceException(String.Format(
38              "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}",
39              typeNameString,
40              type.AssemblyQualifiedName));
41          if (requestedTypeName.IsNewerThan(loadedTypeName))
42            throw new PersistenceException(String.Format(
43              "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
44              typeNameString,
45              type.AssemblyQualifiedName));
46        } catch (PersistenceException) {
47          throw;
48        } catch (Exception e) {
49          Logger.Warn(String.Format(
50            "Could not perform version check requested type was {0} while loaded type is {1}:",
51            typeNameString,
52            type.AssemblyQualifiedName),
53            e);
54        }
55      }
56      return type;
57    }
58  }
59}
Note: See TracBrowser for help on using the repository browser.