Changeset 8585 for branches/HeuristicLab.Mono/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs
- Timestamp:
- 09/06/12 09:52:52 (12 years ago)
- Location:
- branches/HeuristicLab.Mono
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Mono
-
branches/HeuristicLab.Mono/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs
r7586 r8585 261 261 /// <returns>Enumerable of the discovered types.</returns> 262 262 private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions) { 263 var buildTypes = from t in assembly.GetTypes() 264 where CheckTypeCompatibility(type, t) 265 where !IsNonDiscoverableType(t) 266 where onlyInstantiable == false || 267 (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 268 select BuildType(t, type); 269 270 return from t in buildTypes 271 where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition 272 select t; 273 } 274 275 276 private static bool IsNonDiscoverableType(Type t) { 277 return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any(); 278 } 279 280 private static bool CheckTypeCompatibility(Type type, Type other) { 281 if (type.IsAssignableFrom(other)) 282 return true; 283 if (type.IsGenericType && other.IsGenericType) { 284 var otherGenericArguments = other.GetGenericArguments(); 285 var typeGenericArguments = type.GetGenericArguments(); 286 287 //check type arguments count 288 if (otherGenericArguments.Length != typeGenericArguments.Length) 289 return false; 290 291 //check type arguments & constraints 292 int i = 0; 293 foreach (var genericArgument in typeGenericArguments) { 294 if (otherGenericArguments[i].IsGenericParameter) { 295 foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints()) 296 if (!constraint.IsAssignableFrom(genericArgument)) return false; 297 } else if (genericArgument != otherGenericArguments[i]) return false; 298 i++; 299 } 300 //check types 301 try { 302 var otherGenericTypeDefinition = other.GetGenericTypeDefinition(); 303 if (type.IsAssignableFrom(otherGenericTypeDefinition.MakeGenericType(typeGenericArguments))) 304 return true; 305 } 306 catch (Exception) { } 307 } 308 return false; 309 } 310 private static Type BuildType(Type type, Type protoType) { 311 if (type.IsGenericType && protoType.IsGenericType) 312 return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments()); 313 else 314 return type; 263 var matchingTypes = from assemblyType in assembly.GetTypes() 264 let t = assemblyType.BuildType(type) 265 where t != null 266 where t.IsSubTypeOf(type) 267 where !t.IsNonDiscoverableType() 268 where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType) 269 where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition 270 select t; 271 272 return matchingTypes; 315 273 } 316 274
Note: See TracChangeset
for help on using the changeset viewer.