Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.PluginInfrastructure/DiscoveryService.cs @ 15033

Last change on this file since 15033 was 29, checked in by gkronber, 17 years ago
  • refactored pluginInfrastructure
  • fixed #8
File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Reflection;
26
27namespace HeuristicLab.PluginInfrastructure {
28  /// <summary>
29  /// Provides convenience methods to find specific types or to create instances of types.
30  /// </summary>
31  public class DiscoveryService {
32
33    public PluginInfo[] Plugins {
34      get {
35        PluginInfo[] plugins = new PluginInfo[PluginManager.Manager.LoadedPlugins.Count];
36        PluginManager.Manager.LoadedPlugins.CopyTo(plugins, 0);
37        return plugins;
38      }
39    }
40
41    /// <summary>
42    ///  Find all types that are subtypes or equal to the specified type.
43    /// </summary>
44    /// <param name="type">Most general type for which to find matching types.</param>
45    /// <returns></returns>
46    public Type[] GetTypes(Type type) {
47      Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
48      List<Type> types = new List<Type>();
49      foreach(Assembly asm in assemblies) {
50        Array.ForEach<Type>(GetTypes(type, asm), delegate(Type t) {
51          types.Add(t);
52        });
53      }
54      return types.ToArray();
55    }
56
57    /// <summary>
58    /// Create an instance of all types that are subtypes or the same type of the specified type
59    /// </summary>
60    /// <typeparam name="T">Most general type.</typeparam>
61    /// <returns></returns>
62    public T[] GetInstances<T>() where T : class {
63      Type[] types = GetTypes(typeof(T));
64      List<T> instances = new List<T>();
65      foreach(Type t in types) {
66        if(!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
67          instances.Add((T)Activator.CreateInstance(t));
68        }
69      }
70      return instances.ToArray();
71    }
72
73    public Type[] GetTypes(Type type, PluginInfo plugin) {
74      Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
75      List<Type> types = new List<Type>();
76      foreach(Assembly asm in assemblies) {
77        if(plugin.Assemblies.Contains(asm.Location)) {
78          Array.ForEach<Type>(GetTypes(type, asm), delegate(Type t) {
79            types.Add(t);
80          });
81        }
82      }
83
84      return types.ToArray();
85    }
86
87    /// <summary>
88    /// Get instances of all types that implement the specified interface only in the given assembly.
89    /// </summary>
90    /// <typeparam name="T">Interface type.</typeparam>
91    /// <param name="assembly">Assembly that should be searched for types.</param>
92    /// <returns></returns>
93    internal T[] GetInstances<T>(Assembly assembly) {
94      Type[] types = GetTypes(typeof(T), assembly);
95      List<T> instances = new List<T>();
96      foreach(Type t in types) {
97        if(!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
98          instances.Add((T)Activator.CreateInstance(t));
99        }
100      }
101      return instances.ToArray();
102    }
103
104    /// <summary>
105    /// Get types that are assignable (same of subtype) to the specified type only from the given assembly.
106    /// </summary>
107    /// <param name="type">Most general type we want to find.</param>
108    /// <param name="assembly">Assembly that should be searched for types.</param>
109    /// <returns></returns>
110    internal Type[] GetTypes(Type type, Assembly assembly) {
111      List<Type> types = new List<Type>();
112      foreach(Type t in assembly.GetTypes()) {
113        if(type.IsAssignableFrom(t)) {
114          types.Add(t);
115        }
116      }
117      return types.ToArray();
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.