Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2576 was 2234, checked in by mkommend, 15 years ago

implemented non-generic GetInstances method in the DiscoveryService (ticket #718)

File size: 5.8 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    /// Finds 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>The found types as array.</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    /// Creates 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>The created instances as array.</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    /// <summary>
74    /// Creates an instance of all types that are subtypes or the same type of the specified type
75    /// </summary>
76    /// <typeparam name="type">Most general type.</typeparam>
77    /// <returns>The created instances as array.</returns>
78    public object[] GetInstances(Type type) {
79      Type[] types = GetTypes(type);
80      List<object> instances = new List<object>();
81      foreach (Type t in types) {
82        if (!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
83          instances.Add(Activator.CreateInstance(t));
84        }
85      }
86      return instances.ToArray();
87    }
88
89    /// <summary>
90    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
91    /// <paramref name="plugin"/>.
92    /// </summary>
93    /// <param name="type">Most general type for which to find matching types.</param>
94    /// <param name="plugin">The plugin the subtypes must be part of.</param>
95    /// <returns>The found types as array.</returns>
96    public Type[] GetTypes(Type type, PluginInfo plugin) {
97      Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
98      List<Type> types = new List<Type>();
99      foreach (Assembly asm in assemblies) {
100        if (plugin.Assemblies.Contains(asm.Location)) {
101          Array.ForEach<Type>(GetTypes(type, asm), delegate(Type t) {
102            types.Add(t);
103          });
104        }
105      }
106
107      return types.ToArray();
108    }
109
110    /// <summary>
111    /// Gets instances of all types that implement the specified interface only in the given assembly.
112    /// </summary>
113    /// <typeparam name="T">Interface type.</typeparam>
114    /// <param name="assembly">Assembly that should be searched for types.</param>
115    /// <returns>The found instances as array.</returns>
116    internal T[] GetInstances<T>(Assembly assembly) {
117      Type[] types = GetTypes(typeof(T), assembly);
118      List<T> instances = new List<T>();
119      foreach (Type t in types) {
120        if (!t.IsAbstract && !t.IsInterface && !t.HasElementType) {
121          instances.Add((T)Activator.CreateInstance(t));
122        }
123      }
124      return instances.ToArray();
125    }
126
127    /// <summary>
128    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
129    /// </summary>
130    /// <param name="type">Most general type we want to find.</param>
131    /// <param name="assembly">Assembly that should be searched for types.</param>
132    /// <returns>The found types as array.</returns>
133    internal Type[] GetTypes(Type type, Assembly assembly) {
134      List<Type> types = new List<Type>();
135      foreach (Type t in assembly.GetTypes()) {
136        if (type.IsAssignableFrom(t)) {
137          types.Add(t);
138        }
139      }
140      return types.ToArray();
141    }
142
143    /// <summary>
144    /// Gets the plugin of the given <paramref name="type"/>.
145    /// </summary>
146    /// <param name="type"></param>
147    /// <returns>The found plugin or <c>null</c>.</returns>
148    public PluginInfo GetDeclaringPlugin(Type type) {
149      foreach (PluginInfo info in PluginManager.Manager.LoadedPlugins) {
150        if (info.Assemblies.Contains(type.Assembly.Location)) return info;
151      }
152      return null;
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.