Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8699 was 8699, checked in by ascheibe, 12 years ago

#1952 fixed the documentation of the GetMonoType method

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