Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs @ 5850

Last change on this file since 5850 was 5850, checked in by mkommend, 13 years ago

#1454: Implemented TypeDiscovery with multiple base types and adapted TypeSelector and CrossValidation.

File size: 8.7 KB
RevLine 
[3247]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3247]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;
[4068]24using System.Linq;
[3247]25using System.Reflection;
26
27namespace HeuristicLab.PluginInfrastructure {
28
29  /// <summary>
30  /// Lightweight application manager is set as the application manager as long as the plugin infrastructure is uninitialized.
31  /// The list of plugins and applications is empty. The default application manager is necessary to provide the type discovery
32  /// functionality in unit tests.
33  /// </summary>
34  internal sealed class LightweightApplicationManager : IApplicationManager {
[4482]35    internal LightweightApplicationManager() {
36      AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
37    }
[3247]38
[4482]39    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
40      return null;
41    }
42
43
[3247]44    #region IApplicationManager Members
45    /// <summary>
46    /// Gets an empty list of plugins. (LightweightApplicationManager doesn't support plugin discovery)
47    /// </summary>
48    public IEnumerable<IPluginDescription> Plugins {
49      get { return new IPluginDescription[0]; }
50    }
51
52    /// <summary>
53    /// Gets an empty list of applications. (LightweightApplicationManager doesn't support application discovery)
54    /// </summary>
55    public IEnumerable<IApplicationDescription> Applications {
56      get { return new IApplicationDescription[0]; }
57    }
58
59    /// <summary>
60    /// Creates an instance of all types that are subtypes or the same type of the specified type
61    /// </summary>
62    /// <typeparam name="T">Most general type.</typeparam>
63    /// <returns>Enumerable of the created instances.</returns>
64    public IEnumerable<T> GetInstances<T>() where T : class {
65      return GetInstances(typeof(T)).Cast<T>();
66    }
67
68    /// <summary>
69    /// Creates an instance of all types that are subtypes or the same type of the specified type
70    /// </summary>
71    /// <param name="type">Most general type.</param>
72    /// <returns>Enumerable of the created instances.</returns>
73    public IEnumerable<object> GetInstances(Type type) {
[5131]74      List<object> instances = new List<object>();
75      foreach (Type t in GetTypes(type, true)) {
76        object instance = null;
77        try { instance = Activator.CreateInstance(t); }
78        catch { }
79        if (instance != null) instances.Add(instance);
80      }
81      return instances;
[3247]82    }
83
84    /// <summary>
[5850]85    /// Finds all instantiable types that are subtypes or equal to the specified types.
[3247]86    /// </summary>
[5850]87    /// <param name="types">Most general types for which to find matching types.</param>
[3247]88    /// <remarks>Return only types that are instantiable
89    /// (interfaces, abstract classes... are not returned)</remarks>
90    /// <returns>Enumerable of the discovered types.</returns>
[5850]91    public IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable = true, bool allTypes = true) {
92      IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable);
93      foreach (Type type in types.Skip(1)) {
94        IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable);
95        if (allTypes) result = result.Intersect(discoveredTypes);
96        else result = result.Union(discoveredTypes);
97      }
98
99      if (!allTypes) return result.Distinct();
100      return result;
[3247]101    }
102
103    /// <summary>
104    /// Finds all types that are subtypes or equal to the specified type.
105    /// </summary>
106    /// <param name="type">Most general type for which to find matching types.</param>
107    /// <param name="onlyInstantiable">Return only types that are instantiable
108    /// (interfaces, abstract classes... are not returned)</param>
109    /// <returns>Enumerable of the discovered types.</returns>
[5850]110    public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable = true) {
[3247]111      return from asm in AppDomain.CurrentDomain.GetAssemblies()
112             from t in GetTypes(type, asm, onlyInstantiable)
113             select t;
114    }
115
116    /// <summary>
117    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
118    /// </summary>
119    /// <param name="type">Most general type we want to find.</param>
120    /// <param name="assembly">Assembly that should be searched for types.</param>
121    /// <param name="onlyInstantiable">Return only types that are instantiable
122    /// (interfaces, abstract classes...  are not returned)</param>
123    /// <returns>Enumerable of the discovered types.</returns>
[5850]124    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable = true) {
[4482]125      try {
126        var assemblyTypes = assembly.GetTypes();
127
128        return from t in assemblyTypes
129               where CheckTypeCompatibility(type, t)
130               where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
[5741]131               where !IsNonDiscoverableType(t)
[4482]132               select BuildType(t, type);
133      }
134      catch (TypeLoadException) {
135        return Enumerable.Empty<Type>();
136      }
137      catch (ReflectionTypeLoadException) {
138        return Enumerable.Empty<Type>();
139      }
[3247]140    }
141
[5741]142    private static bool IsNonDiscoverableType(Type t) {
143      return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
144    }
145
[3828]146    private static bool CheckTypeCompatibility(Type type, Type other) {
147      if (type.IsAssignableFrom(other))
148        return true;
149      if (type.IsGenericType && other.IsGenericType) {
150        try {
151          if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
152            return true;
153        }
154        catch (Exception) { }
155      }
156      return false;
157    }
158    private static Type BuildType(Type type, Type protoType) {
159      if (type.IsGenericType && protoType.IsGenericType)
160        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
161      else
162        return type;
163    }
164
[3247]165    /// <summary>
166    /// Not supported by the LightweightApplicationManager
167    /// </summary>
168    /// <param name="type"></param>
169    /// <param name="plugin"></param>
170    /// <returns></returns>
171    /// <throws>NotSupportedException</throws>
172    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
173      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
174    }
175
176    /// <summary>
177    /// Not supported by the LightweightApplicationManager
178    /// </summary>
179    /// <param name="type"></param>
180    /// <param name="plugin"></param>
181    /// <param name="onlyInstantiable"></param>
182    /// <returns></returns>
183    /// <throws>NotSupportedException</throws>
[5850]184    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable = true) {
[3247]185      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
186    }
187
188    /// <summary>
189    /// Not supported by the LightweightApplicationManager
190    /// </summary>
191    /// <param name="type"></param>
[5850]192    /// <param name="plugin"></param>
193    /// <param name="onlyInstantiable"></param>
[3247]194    /// <returns></returns>
195    /// <throws>NotSupportedException</throws>
[5850]196    public IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable = true, bool allTypes = true) {
197      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
198    }
199
200    /// <summary>
201    /// Not supported by the LightweightApplicationManager
202    /// </summary>
203    /// <param name="type"></param>
204    /// <returns></returns>
205    /// <throws>NotSupportedException</throws>
[3247]206    public IPluginDescription GetDeclaringPlugin(Type type) {
207      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
208    }
209
210    #endregion
211  }
212}
Note: See TracBrowser for help on using the repository browser.