Ticket #1774: type discovery.patch
File type discovery.patch, 8.3 KB (added by mkommend, 13 years ago) |
---|
-
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs
123 123 /// <returns>Enumerable of the discovered types.</returns> 124 124 private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) { 125 125 try { 126 var assemblyTypes = assembly.GetTypes(); 127 128 var buildTypes = from t in assemblyTypes 126 var buildTypes = from t in assembly.GetTypes() 129 127 where !IsNonDiscoverableType(t) 130 128 where CheckTypeCompatibility(type, t) 131 where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 129 where onlyInstantiable == false || 130 (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 132 131 select BuildType(t, type); 133 132 133 var buildTypes2 = from t in assembly.GetTypes() 134 where !IsNonDiscoverableType(t) 135 where onlyInstantiable == false || 136 (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 137 where CheckTypeCompatibility2(type, t) 138 select BuildType(t, type); 139 140 if (!buildTypes.SequenceEqual(buildTypes2)) throw new InvalidOperationException(type.FullName); 141 134 142 return from t in buildTypes 135 143 where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition 136 144 select t; … … 159 167 } 160 168 return false; 161 169 } 170 171 private static bool CheckTypeCompatibility2(Type type, Type other) { 172 if (type.IsAssignableFrom(other)) 173 return true; 174 if (type.IsGenericType && other.IsGenericType) { 175 var otherGenericArguments = other.GetGenericArguments(); 176 var typeGenericArguments = type.GetGenericArguments(); 177 178 //check type arguments count 179 if (otherGenericArguments.Length != typeGenericArguments.Length) 180 return false; 181 182 //check type arguments & constraints 183 int i = 0; 184 foreach (var genericArgument in typeGenericArguments) { 185 if (otherGenericArguments[i].IsGenericParameter) { 186 foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints()) 187 if (!constraint.IsAssignableFrom(genericArgument)) return false; 188 } else if (genericArgument != otherGenericArguments[i]) return false; 189 i++; 190 } 191 //check types 192 try { 193 var otherGenericTypeDefinition = other.GetGenericTypeDefinition(); 194 if (type.IsAssignableFrom(otherGenericTypeDefinition.MakeGenericType(typeGenericArguments))) 195 return true; 196 } 197 catch (Exception) { } 198 } 199 return false; 200 } 162 201 private static Type BuildType(Type type, Type protoType) { 163 202 if (type.IsGenericType && protoType.IsGenericType) 164 203 return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments()); -
trunk/sources/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs
271 271 (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 272 272 select BuildType(t, type); 273 273 274 var buildTypes2 = from t in assembly.GetTypes() 275 where !IsNonDiscoverableType(t) 276 where onlyInstantiable == false || 277 (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 278 where CheckTypeCompatibility2(type, t) 279 select BuildType(t, type); 280 281 if (!buildTypes.SequenceEqual(buildTypes2)) throw new InvalidOperationException("implementation error"); 282 274 283 return from t in buildTypes 275 284 where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition 276 285 select t; … … 293 302 } 294 303 return false; 295 304 } 305 306 private static bool CheckTypeCompatibility2(Type type, Type other) { 307 if (type.IsAssignableFrom(other)) 308 return true; 309 if (type.IsGenericType && other.IsGenericType) { 310 var otherGenericArguments = other.GetGenericArguments(); 311 var typeGenericArguments = type.GetGenericArguments(); 312 313 //check type arguments count 314 if (otherGenericArguments.Length != typeGenericArguments.Length) 315 return false; 316 317 //check type arguments & constraints 318 int i = 0; 319 foreach (var genericArgument in typeGenericArguments) { 320 if (otherGenericArguments[i].IsGenericParameter) { 321 foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints()) 322 if (!constraint.IsAssignableFrom(genericArgument)) return false; 323 } else if (genericArgument != otherGenericArguments[i]) return false; 324 i++; 325 } 326 //check types 327 try { 328 var otherGenericTypeDefinition = other.GetGenericTypeDefinition(); 329 if (type.IsAssignableFrom(otherGenericTypeDefinition.MakeGenericType(typeGenericArguments))) 330 return true; 331 } 332 catch (Exception) { } 333 } 334 return false; 335 } 296 336 private static Type BuildType(Type type, Type protoType) { 297 337 if (type.IsGenericType && protoType.IsGenericType) 298 338 return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments()); -
trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/PluginDependenciesTest.cs
24 24 using System.Linq; 25 25 using System.Reflection; 26 26 using System.Text; 27 using HeuristicLab.Common; 28 using HeuristicLab.Core; 29 using HeuristicLab.Optimization; 27 30 using HeuristicLab.PluginInfrastructure; 28 31 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 32 … … 90 93 Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString()); 91 94 } 92 95 96 [TestMethod] 97 public void CheckTypeDiscovery() { 98 List<Type> list = new List<Type>(); 99 foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(HeuristicOptimizationProblem<IEvaluator, ISolutionCreator>), false, true)) { 100 list.Add(deepCloneableType); 101 } 102 list.Clear(); 103 104 foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(IValueParameter<>))) { 105 list.Add(deepCloneableType); 106 } 107 list.Clear(); 108 109 foreach (Type deepCloneableType in ApplicationManager.Manager.GetTypes(typeof(IValueParameter<IItem>), false, true)) { 110 list.Add(deepCloneableType); 111 } 112 list.Clear(); 113 114 var objects = new List<object>(); 115 foreach (IDeepCloneable deepCloneable in ApplicationManager.Manager.GetInstances(typeof(IValueParameter<>))) { 116 if (deepCloneable.GetType().FullName.Contains("Hive")) continue; 117 objects.Add(deepCloneable.ToString()); 118 } 119 objects.Clear(); 120 121 foreach (IDeepCloneable deepCloneable in ApplicationManager.Manager.GetInstances(typeof(IValueParameter<Item>))) { 122 if (deepCloneable.GetType().FullName.Contains("Hive")) continue; 123 objects.Add(deepCloneable.ToString()); 124 } 125 objects.Clear(); 126 127 } 128 93 129 private static Type GetPluginFromAssembly(Assembly assembly) { 94 130 return assembly.GetExportedTypes().Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface).FirstOrDefault(); 95 131 }