Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/04/12 16:34:54 (12 years ago)
Author:
mkommend
Message:

#1923:

  • Rewrote type checks for type discovery of plugin infrastructure
  • Extracted common functionality of ApplicationManagers in another class.
  • Added unit tests for the newly implemented methods*
  • Updated test lists to include the new unit tests
Location:
trunk/sources/HeuristicLab.PluginInfrastructure/3.3
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/HeuristicLab.PluginInfrastructure-3.3.csproj

    r8563 r8571  
    272272    <Compile Include="Main.cs" />
    273273    <Compile Include="Properties\AssemblyInfo.cs" />
     274    <Compile Include="TypeExtensions.cs" />
    274275    <None Include="Advanced\DeploymentService\GenerateServiceClients.cmd" />
    275276    <None Include="app.config">
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs

    r8536 r8571  
    7575      foreach (Type t in GetTypes(type)) {
    7676        object instance = null;
    77         try { instance = Activator.CreateInstance(t); } catch { }
     77        try { instance = Activator.CreateInstance(t); }
     78        catch { }
    7879        if (instance != null) instances.Add(instance);
    7980      }
     
    127128        var assemblyTypes = assembly.GetTypes();
    128129
    129         var buildTypes = from t in assembly.GetTypes()
    130                          where CheckTypeCompatibility(type, t)
    131                          where !IsNonDiscoverableType(t)
    132                          where onlyInstantiable == false ||
    133                                (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
    134                          select BuildType(t, type);
     130        var matchingTypes = from assemblyType in assembly.GetTypes()
     131                            let t = assemblyType.BuildType(type)
     132                            where t != null
     133                            where t.IsSubTypeOf(type)
     134                            where !t.IsNonDiscoverableType()
     135                            where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
     136                            where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
     137                            select t;
    135138
    136         return from t in buildTypes
    137                where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
    138                select t;
    139       } catch (TypeLoadException) {
    140         return Enumerable.Empty<Type>();
    141       } catch (ReflectionTypeLoadException) {
     139        return matchingTypes;
     140      }
     141      catch (TypeLoadException) {
    142142        return Enumerable.Empty<Type>();
    143143      }
    144     }
    145 
    146     private static bool IsNonDiscoverableType(Type t) {
    147       return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
    148     }
    149 
    150     private static bool CheckTypeCompatibility(Type type, Type other) {
    151       if (type.IsAssignableFrom(other))
    152         return true;
    153       if (type.IsGenericType && other.IsGenericType) {
    154         var otherGenericArguments = other.GetGenericArguments();
    155         var typeGenericArguments = type.GetGenericArguments();
    156 
    157         //check type arguments count
    158         if (otherGenericArguments.Length != typeGenericArguments.Length)
    159           return false;
    160 
    161         //check type arguments & constraints
    162         int i = 0;
    163         foreach (var genericArgument in typeGenericArguments) {
    164           if (otherGenericArguments[i].IsGenericParameter) {
    165             foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints())
    166               if (!constraint.IsAssignableFrom(genericArgument)) return false;
    167           } else if (genericArgument != otherGenericArguments[i]) return false;
    168           i++;
    169         }
    170         //check types
    171         try {
    172           var otherGenericTypeDefinition = other.GetGenericTypeDefinition();
    173           if (type.IsAssignableFrom(otherGenericTypeDefinition.MakeGenericType(typeGenericArguments)))
    174             return true;
    175         } catch (Exception) { }
     144      catch (ReflectionTypeLoadException) {
     145        return Enumerable.Empty<Type>();
    176146      }
    177       return false;
    178     }
    179     private static Type BuildType(Type type, Type protoType) {
    180       if (type.IsGenericType && protoType.IsGenericType)
    181         return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
    182       else
    183         return type;
    184147    }
    185148
  • trunk/sources/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs

    r8536 r8571  
    261261    /// <returns>Enumerable of the discovered types.</returns>
    262262    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;
    315273    }
    316274
Note: See TracChangeset for help on using the changeset viewer.