Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3743 was 3743, checked in by gkronber, 14 years ago

Fixed GPL license headers #893

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System;
24using HeuristicLab.Persistence.Interfaces;
25using HeuristicLab.Persistence.Core.Tokens;
26using HeuristicLab.Persistence.Auxiliary;
27using HeuristicLab.Tracing;
28using System.Reflection;
29using HeuristicLab.Persistence.Core;
30
31namespace HeuristicLab.Persistence.Auxiliary {
32
33  internal class TypeLoader {
34
35    public static Type Load(string typeNameString) {
36      Type type;
37      try {
38        type = Type.GetType(typeNameString, true);
39      } catch (Exception) {
40        Logger.Warn(String.Format(
41          "Cannot load type \"{0}\", falling back to partial name", typeNameString));
42        try {
43          TypeName typeName = TypeNameParser.Parse(typeNameString);             
44#pragma warning disable 0618
45          Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);         
46          // the suggested Assembly.Load() method fails to load assemblies outside the GAC
47#pragma warning restore 0618
48          type = a.GetType(typeName.ToString(false, false), true);
49        } catch (Exception) {
50          throw new PersistenceException(String.Format(
51            "Could not load type \"{0}\"",
52            typeNameString));
53        }
54        try {
55          TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
56          TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
57          if (!requestedTypeName.IsCompatible(loadedTypeName))
58            throw new PersistenceException(String.Format(
59              "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}",
60              typeNameString,
61              type.AssemblyQualifiedName));
62          if (requestedTypeName.IsNewerThan(loadedTypeName))
63            throw new PersistenceException(String.Format(
64              "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
65              typeNameString,
66              type.AssemblyQualifiedName));
67        } catch (PersistenceException) {
68          throw;
69        } catch (Exception e) {
70          Logger.Warn(String.Format(
71            "Could not perform version check requested type was {0} while loaded type is {1}:",
72            typeNameString,
73            type.AssemblyQualifiedName),
74            e);
75        }
76      }
77      return type;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.