Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/19/10 13:40:52 (14 years ago)
Author:
gkronber
Message:

Added a two versions of the type discovery methods in IApplicationManager with a boolean parameter to select if only instantiable types should be returned. #846

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/ApplicationManager.cs

    r2594 r2642  
    157157    /// <returns>Enumerable of the created instances.</returns>
    158158    internal static IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
    159       return from t in GetTypes(typeof(T), plugin)
     159      return from t in GetTypes(typeof(T), plugin, true)
    160160             select (T)Activator.CreateInstance(t);
    161161    }
     
    167167    /// <returns>Enumerable of the created instances.</returns>
    168168    private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {
    169       return from t in GetTypes(typeof(T), asm)
     169      return from t in GetTypes(typeof(T), asm, true)
    170170             select (T)Activator.CreateInstance(t);
    171171    }
     
    186186    /// <returns>Enumerable of the created instances.</returns>
    187187    internal static IEnumerable<object> GetInstances(Type type) {
    188       return from t in GetTypes(type)
     188      return from t in GetTypes(type, true)
    189189             select Activator.CreateInstance(t);
    190190    }
     
    194194    /// </summary>
    195195    /// <param name="type">Most general type for which to find matching types.</param>
     196    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
    196197    /// <returns>Enumerable of the discovered types.</returns>
    197     internal static IEnumerable<Type> GetTypes(Type type) {
     198    internal static IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
    198199      return from asm in AppDomain.CurrentDomain.GetAssemblies()
    199              from t in GetTypes(type, asm)
     200             from t in GetTypes(type, asm, onlyInstantiable)
    200201             select t;
    201202    }
     
    207208    /// <param name="type">Most general type for which to find matching types.</param>
    208209    /// <param name="plugin">The plugin the subtypes must be part of.</param>
     210    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
    209211    /// <returns>Enumerable of the discovered types.</returns>
    210     internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription) {
     212    internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription, bool onlyInstantiable) {
    211213      PluginDescription pluginDesc = (PluginDescription)pluginDescription;
    212214      return from asm in AppDomain.CurrentDomain.GetAssemblies()
    213              where !string.IsNullOrEmpty(asm.Location ) &&
     215             where !string.IsNullOrEmpty(asm.Location) &&
    214216                   pluginDesc.Assemblies.Any(asmPath => Path.GetFullPath(asmPath) == Path.GetFullPath(asm.Location))
    215              from t in GetTypes(type, asm)
     217             from t in GetTypes(type, asm, onlyInstantiable)
    216218             select t;
    217219    }
     
    222224    /// <param name="type">Most general type we want to find.</param>
    223225    /// <param name="assembly">Assembly that should be searched for types.</param>
     226    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
    224227    /// <returns>Enumerable of the discovered types.</returns>
    225     private static IEnumerable<Type> GetTypes(Type type, Assembly assembly) {
    226       return GetTypes(type, assembly, false);
    227     }
    228 
    229     private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool includeNotInstantiableTypes) {
     228    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
    230229      return from t in assembly.GetTypes()
    231230             where type.IsAssignableFrom(t)
    232              where includeNotInstantiableTypes || (type.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface && !t.HasElementType)
     231             where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
    233232             select t;
    234233    }
     
    253252    #region IApplicationManager Members
    254253
    255     IEnumerable<T> IApplicationManager.GetInstances<T>(IPluginDescription plugin) {
    256       return GetInstances<T>(plugin);
    257     }
    258 
    259254    IEnumerable<T> IApplicationManager.GetInstances<T>() {
    260255      return GetInstances<T>();
     
    266261
    267262    IEnumerable<Type> IApplicationManager.GetTypes(Type type) {
    268       return GetTypes(type);
     263      return GetTypes(type, true);
     264    }
     265
     266    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable) {
     267      return GetTypes(type, onlyInstantiable);
    269268    }
    270269
    271270    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin) {
    272       return GetTypes(type, plugin);
    273     }
     271      return GetTypes(type, plugin, true);
     272    }
     273
     274    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
     275      return GetTypes(type, plugin, onlyInstantiable);
     276    }
     277
    274278
    275279    /// <summary>
Note: See TracChangeset for help on using the changeset viewer.