Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/DiscoveryService.cs @ 23

Last change on this file since 23 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.1 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        return PluginManager.Manager.LoadedPlugins;
36      }
37    }
38
39    /// <summary>
40    ///  Find all types that are subtypes or equal to the specified type.
41    /// </summary>
42    /// <param name="type">Most general type for which to find matching types.</param>
43    /// <returns></returns>
44    public Type[] GetTypes(Type type) {
45      Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
46      List<Type> types = new List<Type>();
47      foreach(Assembly asm in assemblies) {
48        Array.ForEach<Type>(GetTypes(type, asm), delegate(Type t) {
49          types.Add(t);
50        });
51      }
52      return types.ToArray();
53    }
54
55    /// <summary>
56    /// Create an instance of all types that are subtypes or the same type of the specified type
57    /// </summary>
58    /// <typeparam name="T">Most general type.</typeparam>
59    /// <returns></returns>
60    public T[] GetInstances<T>() where T : class {
61      Type[] types = GetTypes(typeof(T));
62      List<T> instances = new List<T>();
63      foreach(Type t in types) {
64        if(!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
65          instances.Add((T)Activator.CreateInstance(t));
66        }
67      }
68      return instances.ToArray();
69    }
70
71    public Type[] GetTypes(Type type, PluginInfo plugin) {
72      Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
73      List<Type> types = new List<Type>();
74      foreach(Assembly asm in assemblies) {
75        if(plugin.Assemblies.Contains(asm.Location)) {
76          Array.ForEach<Type>(GetTypes(type, asm), delegate(Type t) {
77            types.Add(t);
78          });
79        }
80      }
81
82      return types.ToArray();
83    }
84
85    /// <summary>
86    /// Get instances of all types that implement the specified interface only in the given assembly.
87    /// </summary>
88    /// <typeparam name="T">Interface type.</typeparam>
89    /// <param name="assembly">Assembly that should be searched for types.</param>
90    /// <returns></returns>
91    internal T[] GetInstances<T>(Assembly assembly) {
92      Type[] types = GetTypes(typeof(T), assembly);
93      List<T> instances = new List<T>();
94      foreach(Type t in types) {
95        if(!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
96          instances.Add((T)Activator.CreateInstance(t));
97        }
98      }
99      return instances.ToArray();
100    }
101
102    /// <summary>
103    /// Get types that are assignable (same of subtype) to the specified type only from the given assembly.
104    /// </summary>
105    /// <param name="type">Most general type we want to find.</param>
106    /// <param name="assembly">Assembly that should be searched for types.</param>
107    /// <returns></returns>
108    internal Type[] GetTypes(Type type, Assembly assembly) {
109      List<Type> types = new List<Type>();
110      foreach(Type t in assembly.GetTypes()) {
111        if(type.IsAssignableFrom(t)) {
112          types.Add(t);
113        }
114      }
115      return types.ToArray();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.