Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7259 was 7259, checked in by swagner, 12 years ago

Updated year of copyrights to 2012 (#1716)

File size: 3.0 KB
RevLine 
[3743]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3743]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
[1454]22using System;
[1682]23using System.Reflection;
[1823]24using HeuristicLab.Persistence.Core;
[4068]25using HeuristicLab.Tracing;
[1454]26
[1823]27namespace HeuristicLab.Persistence.Auxiliary {
[1454]28
[3004]29  internal class TypeLoader {
[1780]30
31    public static Type Load(string typeNameString) {
32      Type type;
33      try {
34        type = Type.GetType(typeNameString, true);
[4068]35      }
36      catch (Exception) {
[1780]37        Logger.Warn(String.Format(
[1795]38          "Cannot load type \"{0}\", falling back to partial name", typeNameString));
[1780]39        try {
[4068]40          TypeName typeName = TypeNameParser.Parse(typeNameString);
[3036]41#pragma warning disable 0618
[4068]42          Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);
[3036]43          // the suggested Assembly.Load() method fails to load assemblies outside the GAC
44#pragma warning restore 0618
[1780]45          type = a.GetType(typeName.ToString(false, false), true);
[4068]46        }
47        catch (Exception) {
[1780]48          throw new PersistenceException(String.Format(
49            "Could not load type \"{0}\"",
50            typeNameString));
51        }
52        try {
[1795]53          TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
54          TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
55          if (!requestedTypeName.IsCompatible(loadedTypeName))
[1780]56            throw new PersistenceException(String.Format(
[1795]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(
[1780]62              "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
63              typeNameString,
64              type.AssemblyQualifiedName));
[4068]65        }
66        catch (PersistenceException) {
[1780]67          throw;
[4068]68        }
69        catch (Exception e) {
[1780]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  }
[1454]80}
Note: See TracBrowser for help on using the repository browser.