Changeset 8698 for trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary
- Timestamp:
- 09/25/12 12:29:13 (12 years ago)
- 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 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Reflection; 24 25 using HeuristicLab.Persistence.Core; … … 27 28 namespace HeuristicLab.Persistence.Auxiliary { 28 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 29 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 30 56 try { 31 57 // try to load type normally 32 return LoadInternal(typeName String);58 return LoadInternal(typeName); 33 59 } 34 60 catch (PersistenceException) { 35 61 #region Mono Compatibility 36 62 // if that fails, try to load Mono type 37 string monoTypeNameString = GetMonoType(typeNameString);63 TypeName monoTypeName = GetMonoType(typeName); 38 64 Logger.Info(String.Format(@"Trying to load Mono type ""{0}"" instead of type ""{1}""", 39 monoTypeName String, typeNameString));40 return LoadInternal(monoTypeName String);65 monoTypeName, typeNameString)); 66 return LoadInternal(monoTypeName); 41 67 } 42 68 #endregion 43 69 } 44 70 45 private static Type LoadInternal( string typeNameString) {71 private static Type LoadInternal(TypeName typeName) { 46 72 Type type; 47 73 try { 48 type = Type.GetType(typeName String, true);49 } 74 type = Type.GetType(typeName.ToString(true, true), true); 75 } 50 76 catch (Exception) { 51 77 Logger.Warn(String.Format( 52 "Cannot load type \"{0}\", falling back to partial name", typeName String));53 type = LoadWithPartialName(typeName String);54 CheckCompatibility(typeName String, type);78 "Cannot load type \"{0}\", falling back to partial name", typeName.ToString(true, true))); 79 type = LoadWithPartialName(typeName); 80 CheckCompatibility(typeName, type); 55 81 } 56 82 return type; 57 83 } 58 84 59 private static Type LoadWithPartialName( string typeNameString) {85 private static Type LoadWithPartialName(TypeName typeName) { 60 86 try { 61 TypeName typeName = TypeNameParser.Parse(typeNameString);62 87 #pragma warning disable 0618 63 88 Assembly a = Assembly.LoadWithPartialName(typeName.AssemblyName); … … 69 94 throw new PersistenceException(String.Format( 70 95 "Could not load type \"{0}\"", 71 typeName String));96 typeName.ToString(true, true))); 72 97 } 73 98 } 74 99 75 private static void CheckCompatibility( string typeNameString, Type type) {100 private static void CheckCompatibility(TypeName typeName, Type type) { 76 101 try { 77 TypeName requestedTypeName = TypeNameParser.Parse(typeNameString);78 102 TypeName loadedTypeName = TypeNameParser.Parse(type.AssemblyQualifiedName); 79 if (! requestedTypeName.IsCompatible(loadedTypeName))103 if (!typeName.IsCompatible(loadedTypeName)) 80 104 throw new PersistenceException(String.Format( 81 105 "Serialized type is incompatible with available type: serialized: {0}, loaded: {1}", 82 typeName String,106 typeName.ToString(true, true), 83 107 type.AssemblyQualifiedName)); 84 if ( requestedTypeName.IsNewerThan(loadedTypeName))108 if (typeName.IsNewerThan(loadedTypeName)) 85 109 throw new PersistenceException(String.Format( 86 110 "Serialized type is newer than available type: serialized: {0}, loaded: {1}", 87 typeName String,111 typeName.ToString(true, true), 88 112 type.AssemblyQualifiedName)); 89 113 } … … 94 118 Logger.Warn(String.Format( 95 119 "Could not perform version check requested type was {0} while loaded type is {1}:", 96 typeName String,120 typeName.ToString(true, true), 97 121 type.AssemblyQualifiedName), 98 122 e); … … 107 131 /// The remapped typeNameString, or the original string if no mapping was found 108 132 /// </returns> 109 private static string GetMonoType(string typeNameString) { 110 TypeName typeName = TypeNameParser.Parse(typeNameString); 111 133 private static TypeName GetMonoType(TypeName typeName) { 112 134 // map System.RuntimeType to System.MonoType 113 135 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 117 138 } 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; 127 142 } 128 129 return typeName.ToString(true, true); 143 return typeName; 130 144 } 131 145 #endregion -
trunk/sources/HeuristicLab.Persistence/3.3/Auxiliary/TypeName.cs
r8641 r8698 41 41 /// <value>The namespace.</value> 42 42 [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; } 47 44 48 45 /// <summary> … … 51 48 /// <value>The name of the class.</value> 52 49 [Storable] 53 #region Mono Compatibility 54 public string ClassName { get; internal set; } 55 #endregion 50 public string ClassName { get; private set; } 56 51 57 52 /// <summary> … … 116 111 } 117 112 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 } 118 126 119 127 /// <summary>
Note: See TracChangeset
for help on using the changeset viewer.