Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.PluginInfrastructure/DiscoveryService.cs @ 3921

Last change on this file since 3921 was 1189, checked in by vdorfer, 16 years ago

Created parts of the API documentation for HeuristicLab.PluginInfrastructure namespace (#331)

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