Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Persistence/3.3/Auxiliary/TypeLoader.cs @ 7268

Last change on this file since 7268 was 7268, checked in by gkronber, 12 years ago

#1081: merged r7214:7266 from trunk into time series branch.

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Reflection;
24using HeuristicLab.Persistence.Core;
25using HeuristicLab.Tracing;
26
27namespace HeuristicLab.Persistence.Auxiliary {
28
29  internal class TypeLoader {
30
31    public static Type Load(string typeNameString) {
32      Type type;
33      try {
34        type = Type.GetType(typeNameString, true);
35      }
36      catch (Exception) {
37        Logger.Warn(String.Format(
38          "Cannot load type \"{0}\", falling back to partial name", typeNameString));
39        try {
40          TypeName typeName = TypeNameParser.Parse(typeNameString);
41#pragma warning disable 0618
42          Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);
43          // the suggested Assembly.Load() method fails to load assemblies outside the GAC
44#pragma warning restore 0618
45          type = a.GetType(typeName.ToString(false, false), true);
46        }
47        catch (Exception) {
48          throw new PersistenceException(String.Format(
49            "Could not load type \"{0}\"",
50            typeNameString));
51        }
52        try {
53          TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
54          TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
55          if (!requestedTypeName.IsCompatible(loadedTypeName))
56            throw new PersistenceException(String.Format(
57              "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}",
58              typeNameString,
59              type.AssemblyQualifiedName));
60          if (requestedTypeName.IsNewerThan(loadedTypeName))
61            throw new PersistenceException(String.Format(
62              "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
63              typeNameString,
64              type.AssemblyQualifiedName));
65        }
66        catch (PersistenceException) {
67          throw;
68        }
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.