Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8647 was 8647, checked in by ascheibe, 10 years ago

#1952 removed mono compatibility code that doesn't seem to be necessary and caused the GA performance unit test to fail

File size: 5.5 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  internal class TypeLoader {
29    public static Type Load(string typeNameString) {
30      try {
31        // try to load type normally
32        return LoadInternal(typeNameString);
33      }
34      catch (PersistenceException) {
35        #region Mono Compatibility
36        // if that fails, try to load Mono type
37        string monoTypeNameString = GetMonoType(typeNameString);
38        Logger.Info(String.Format(@"Trying to load Mono type ""{0}"" instead of type ""{1}""",
39                                  monoTypeNameString, typeNameString));
40        return LoadInternal(monoTypeNameString);
41      }
42        #endregion
43    }
44
45    private static Type LoadInternal(string typeNameString) {
46      Type type;
47      try {
48        type = Type.GetType(typeNameString, true);
49      }     
50      catch (Exception) {
51        Logger.Warn(String.Format(
52          "Cannot load type \"{0}\", falling back to partial name", typeNameString));
53        type = LoadWithPartialName(typeNameString);
54        CheckCompatibility(typeNameString, type);
55      }
56      return type;
57    }
58
59    private static Type LoadWithPartialName(string typeNameString) {
60      try {
61        TypeName typeName = TypeNameParser.Parse(typeNameString);
62#pragma warning disable 0618
63        Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);
64        // the suggested Assembly.Load() method fails to load assemblies outside the GAC
65#pragma warning restore 0618
66        return a.GetType(typeName.ToString(false, false), true);
67      }
68      catch (Exception) {
69        throw new PersistenceException(String.Format(
70          "Could not load type \"{0}\"",
71          typeNameString));
72      }
73    }
74
75    private static void CheckCompatibility(string typeNameString, Type type) {
76      try {
77        TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
78        TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
79        if (!requestedTypeName.IsCompatible(loadedTypeName))
80          throw new PersistenceException(String.Format(
81            "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}",
82            typeNameString,
83            type.AssemblyQualifiedName));
84        if (requestedTypeName.IsNewerThan(loadedTypeName))
85          throw new PersistenceException(String.Format(
86            "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
87            typeNameString,
88            type.AssemblyQualifiedName));
89      }
90      catch (PersistenceException) {
91        throw;
92      }
93      catch (Exception e) {
94        Logger.Warn(String.Format(
95          "Could not perform version check requested type was {0} while loaded type is {1}:",
96          typeNameString,
97          type.AssemblyQualifiedName),
98                    e);
99      }
100    }
101
102    #region Mono Compatibility
103    /// <summary>
104    /// Returns the corresponding type for the Mono runtime
105    /// </summary>
106    /// <returns>
107    /// The remapped typeNameString, or the original string if no mapping was found
108    /// </returns>
109    private static string GetMonoType(string typeNameString) {
110      TypeName typeName = TypeNameParser.Parse(typeNameString);
111
112      // map System.RuntimeType to System.MonoType
113      if (typeName.Namespace == "System" && typeName.ClassName == "RuntimeType") {
114        // we use Int32 here because we get all the information about Mono's mscorlib and just have to change the class name
115        typeName = TypeNameParser.Parse(typeof(System.Int32).AssemblyQualifiedName);
116        typeName.ClassName = "MonoType";
117      } else if (typeName.Namespace == "System.Collections.Generic" && typeName.ClassName == "ObjectEqualityComparer") {
118        // map System.Collections.Generic.ObjectEqualityComparer to HeuristicLab.Mono.ObjectEqualityComparer       
119        // we need the information about the Persistence assembly, so we use TypeName here because it is contained in this assembly
120        TypeName oecInfo = TypeNameParser.Parse(typeof(TypeName).AssemblyQualifiedName);
121        typeName.Namespace = "HeuristicLab.Persistence.Mono";
122        typeName.AssemblyName = oecInfo.AssemblyName;
123        typeName.AssemblyAttribues.Clear();
124        typeName.AssemblyAttribues["Version"] = oecInfo.AssemblyAttribues["Version"];
125        typeName.AssemblyAttribues["Culture"] = oecInfo.AssemblyAttribues["Culture"];
126        typeName.AssemblyAttribues["PublicKeyToken"] = oecInfo.AssemblyAttribues["PublicKeyToken"];
127      }
128
129      return typeName.ToString(true, true);
130    }
131    #endregion
132  }
133}
Note: See TracBrowser for help on using the repository browser.