Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2642 for trunk


Ignore:
Timestamp:
01/19/10 13:40:52 (15 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

Location:
trunk/sources/HeuristicLab.PluginInfrastructure
Files:
2 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>
  • trunk/sources/HeuristicLab.PluginInfrastructure/Interfaces/IApplicationManager.cs

    r2592 r2642  
    3535    /// </summary>
    3636    IEnumerable<IPluginDescription> Plugins { get; }
     37
    3738    /// <summary>
    3839    /// Gets all discovered applications.
     
    4041    IEnumerable<IApplicationDescription> Applications { get; }
    4142
    42     /// <summary>
    43     /// Discovers and creates instances of <typeparamref name="T"/> and all types implementing or inheriting <typeparamref name="T"/> (directly and indirectly) declared in any assembly of <paramref name="plugin"/>.
    44     /// </summary>
    45     /// <typeparam name="T">The type or super-type to discover.</typeparam>
    46     /// <param name="plugin">The declaring plugin.</param>
    47     /// <returns>An enumerable of instances of the discovered types.</returns>
    48     IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class;
    4943    /// <summary>
    5044    /// Discovers and creates instances of <typeparamref name="T"/> and all types implementing or inheriting <typeparamref name="T"/> (directly and indirectly).
     
    6761    /// <returns>An enumerable of discovered types.</returns>
    6862    IEnumerable<Type> GetTypes(Type type);
     63
     64    /// <summary>
     65    /// Discovers all types implementing or inheriting <paramref name="type"/> (directly and indirectly).
     66    /// </summary>
     67    /// <param name="type">The type to discover.</param>
     68    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
     69    /// <returns>An enumerable of discovered types.</returns>
     70    IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable);
     71
    6972    /// <summary>
    7073    /// Discovers all types implementing or inheriting <paramref name="type"/> (directly and indirectly) that are declaed in any assembly of <paramref name="plugin"/>.
     
    7477    /// <returns>An enumerable of discovered types.</returns>
    7578    IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin);
     79
     80    /// <summary>
     81    /// Discovers all types implementing or inheriting <paramref name="type"/> (directly and indirectly) that are declaed in any assembly of <paramref name="plugin"/>.
     82    /// </summary>
     83    /// <param name="type">The type to discover.</param>
     84    /// <param name="plugin">The declaring plugin.</param>
     85    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
     86    /// <returns>An enumerable of discovered types.</returns>
     87    IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable);
     88
    7689    /// <summary>
    7790    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
Note: See TracChangeset for help on using the changeset viewer.