#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace HeuristicLab.PluginInfrastructure {
///
/// Provides convenience methods to find specific types or to create instances of types.
///
public class DiscoveryService {
public PluginInfo[] Plugins {
get {
PluginInfo[] plugins = new PluginInfo[PluginManager.Manager.LoadedPlugins.Count];
PluginManager.Manager.LoadedPlugins.CopyTo(plugins, 0);
return plugins;
}
}
///
/// Finds all types that are subtypes or equal to the specified type.
///
/// Most general type for which to find matching types.
/// The found types as array.
public Type[] GetTypes(Type type) {
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
List types = new List();
foreach (Assembly asm in assemblies) {
Array.ForEach(GetTypes(type, asm), delegate(Type t) {
types.Add(t);
});
}
return types.ToArray();
}
///
/// Creates an instance of all types that are subtypes or the same type of the specified type
///
/// Most general type.
/// The created instances as array.
public T[] GetInstances() where T : class {
Type[] types = GetTypes(typeof(T));
List instances = new List();
foreach (Type t in types) {
if (!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
instances.Add((T)Activator.CreateInstance(t));
}
}
return instances.ToArray();
}
///
/// Creates an instance of all types that are subtypes or the same type of the specified type
///
/// Most general type.
/// The created instances as array.
public object[] GetInstances(Type type) {
Type[] types = GetTypes(type);
List