Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.PluginInfrastructure/DiscoveryService.cs @ 870

Last change on this file since 870 was 638, checked in by gkronber, 16 years ago

created a branch for changes needed to run HL3 on Mono 2.0
(ticket #298)

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