Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8698 for trunk/sources


Ignore:
Timestamp:
09/25/12 12:29:13 (12 years ago)
Author:
ascheibe
Message:

#1952 implemented reviewing comments

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeLoader.cs

    r8647 r8698  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Reflection;
    2425using HeuristicLab.Persistence.Core;
     
    2728namespace HeuristicLab.Persistence.Auxiliary {
    2829  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
    2945    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
    3056      try {
    3157        // try to load type normally
    32         return LoadInternal(typeNameString);
     58        return LoadInternal(typeName);
    3359      }
    3460      catch (PersistenceException) {
    3561        #region Mono Compatibility
    3662        // if that fails, try to load Mono type
    37         string monoTypeNameString = GetMonoType(typeNameString);
     63        TypeName monoTypeName = GetMonoType(typeName);
    3864        Logger.Info(String.Format(@"Trying to load Mono type ""{0}"" instead of type ""{1}""",
    39                                   monoTypeNameString, typeNameString));
    40         return LoadInternal(monoTypeNameString);
     65                                  monoTypeName, typeNameString));
     66        return LoadInternal(monoTypeName);
    4167      }
    4268        #endregion
    4369    }
    4470
    45     private static Type LoadInternal(string typeNameString) {
     71    private static Type LoadInternal(TypeName typeName) {
    4672      Type type;
    4773      try {
    48         type = Type.GetType(typeNameString, true);
    49       }     
     74        type = Type.GetType(typeName.ToString(true, true), true);
     75      }
    5076      catch (Exception) {
    5177        Logger.Warn(String.Format(
    52           "Cannot load type \"{0}\", falling back to partial name", typeNameString));
    53         type = LoadWithPartialName(typeNameString);
    54         CheckCompatibility(typeNameString, type);
     78          "Cannot load type \"{0}\", falling back to partial name", typeName.ToString(true, true)));
     79        type = LoadWithPartialName(typeName);
     80        CheckCompatibility(typeName, type);
    5581      }
    5682      return type;
    5783    }
    5884
    59     private static Type LoadWithPartialName(string typeNameString) {
     85    private static Type LoadWithPartialName(TypeName typeName) {
    6086      try {
    61         TypeName typeName = TypeNameParser.Parse(typeNameString);
    6287#pragma warning disable 0618
    6388        Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName);
     
    6994        throw new PersistenceException(String.Format(
    7095          "Could not load type \"{0}\"",
    71           typeNameString));
     96          typeName.ToString(true, true)));
    7297      }
    7398    }
    7499
    75     private static void CheckCompatibility(string typeNameString, Type type) {
     100    private static void CheckCompatibility(TypeName typeName, Type type) {
    76101      try {
    77         TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);
    78102        TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName);
    79         if (!requestedTypeName.IsCompatible(loadedTypeName))
     103        if (!typeName.IsCompatible(loadedTypeName))
    80104          throw new PersistenceException(String.Format(
    81105            "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}",
    82             typeNameString,
     106            typeName.ToString(true, true),
    83107            type.AssemblyQualifiedName));
    84         if (requestedTypeName.IsNewerThan(loadedTypeName))
     108        if (typeName.IsNewerThan(loadedTypeName))
    85109          throw new PersistenceException(String.Format(
    86110            "Serialized type is newer than available type: serialized: {0}, loaded: {1}",
    87             typeNameString,
     111            typeName.ToString(true, true),
    88112            type.AssemblyQualifiedName));
    89113      }
     
    94118        Logger.Warn(String.Format(
    95119          "Could not perform version check requested type was {0} while loaded type is {1}:",
    96           typeNameString,
     120          typeName.ToString(true, true),
    97121          type.AssemblyQualifiedName),
    98122                    e);
     
    107131    /// The remapped typeNameString, or the original string if no mapping was found
    108132    /// </returns>
    109     private static string GetMonoType(string typeNameString) {
    110       TypeName typeName = TypeNameParser.Parse(typeNameString);
    111 
     133    private static TypeName GetMonoType(TypeName typeName) {
    112134      // map System.RuntimeType to System.MonoType
    113135      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";
     136        return cachedRuntimeType;
     137        // map System.Collections.Generic.ObjectEqualityComparer to HeuristicLab.Mono.ObjectEqualityComparer
    117138      } 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"];
     139        TypeName newTypeName = new TypeName(cachedObjectEqualityComparerType);
     140        newTypeName.GenericArgs = new List<TypeName>(typeName.GenericArgs);
     141        return newTypeName;
    127142      }
    128 
    129       return typeName.ToString(true, true);
     143      return typeName;
    130144    }
    131145    #endregion
  • trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeName.cs

    r8641 r8698  
    4141    /// <value>The namespace.</value>
    4242    [Storable]
    43     #region Mono Compatibility
    44     // mono: setting the namespace is needed for generating ObjectEqualityComparer type names in the TypeLoader
    45     public string Namespace { get; internal set; }
    46     #endregion
     43    public string Namespace { get; private set; }
    4744
    4845    /// <summary>
     
    5148    /// <value>The name of the class.</value>
    5249    [Storable]
    53     #region Mono Compatibility
    54     public string ClassName { get; internal set; }
    55     #endregion
     50    public string ClassName { get; private set; }
    5651
    5752    /// <summary>
     
    116111    }
    117112
     113    internal TypeName(TypeName typeName, string className = null, string nameSpace = null) {
     114      Namespace = typeName.Namespace;
     115      ClassName = typeName.ClassName;
     116      GenericArgs = new List<TypeName>(typeName.GenericArgs);
     117      AssemblyAttribues = new Dictionary<string, string>(typeName.AssemblyAttribues);
     118      MemoryMagic = typeName.MemoryMagic;
     119      AssemblyName = typeName.AssemblyName;
     120      IsReference = typeName.IsReference;
     121      if (nameSpace != null)
     122        Namespace = nameSpace;
     123      if (className != null)
     124        ClassName = className;
     125    }
    118126
    119127    /// <summary>
Note: See TracChangeset for help on using the changeset viewer.