Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs @ 6215

Last change on this file since 6215 was 4482, checked in by gkronber, 14 years ago

Preparations for next HL release. #1203

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
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 {
35    internal LightweightApplicationManager() {
36      AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
37    }
38
39    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
40      return null;
41    }
42
43
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) {
74      return from t in GetTypes(type, true)
75             select Activator.CreateInstance(t);
76    }
77
78
79    /// <summary>
80    /// Finds all instantiable types that are subtypes or equal to the specified type.
81    /// </summary>
82    /// <param name="type">Most general type for which to find matching types.</param>
83    /// <remarks>Return only types that are instantiable
84    /// (interfaces, abstract classes... are not returned)</remarks>
85    /// <returns>Enumerable of the discovered types.</returns>
86    public IEnumerable<Type> GetTypes(Type type) {
87      return GetTypes(type, true);
88    }
89
90    /// <summary>
91    /// Finds all types that are subtypes or equal to the specified type.
92    /// </summary>
93    /// <param name="type">Most general type for which to find matching types.</param>
94    /// <param name="onlyInstantiable">Return only types that are instantiable
95    /// (interfaces, abstract classes... are not returned)</param>
96    /// <returns>Enumerable of the discovered types.</returns>
97    public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
98      return from asm in AppDomain.CurrentDomain.GetAssemblies()
99             from t in GetTypes(type, asm, onlyInstantiable)
100             select t;
101    }
102
103    /// <summary>
104    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
105    /// </summary>
106    /// <param name="type">Most general type we want to find.</param>
107    /// <param name="assembly">Assembly that should be searched for types.</param>
108    /// <param name="onlyInstantiable">Return only types that are instantiable
109    /// (interfaces, abstract classes...  are not returned)</param>
110    /// <returns>Enumerable of the discovered types.</returns>
111    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
112      try {
113        var assemblyTypes = assembly.GetTypes();
114
115        return from t in assemblyTypes
116               where CheckTypeCompatibility(type, t)
117               where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
118               select BuildType(t, type);
119      }
120      catch (TypeLoadException) {
121        return Enumerable.Empty<Type>();
122      }
123      catch (ReflectionTypeLoadException) {
124        return Enumerable.Empty<Type>();
125      }
126    }
127
128    private static bool CheckTypeCompatibility(Type type, Type other) {
129      if (type.IsAssignableFrom(other))
130        return true;
131      if (type.IsGenericType && other.IsGenericType) {
132        try {
133          if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
134            return true;
135        }
136        catch (Exception) { }
137      }
138      return false;
139    }
140    private static Type BuildType(Type type, Type protoType) {
141      if (type.IsGenericType && protoType.IsGenericType)
142        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
143      else
144        return type;
145    }
146
147    /// <summary>
148    /// Not supported by the LightweightApplicationManager
149    /// </summary>
150    /// <param name="type"></param>
151    /// <param name="plugin"></param>
152    /// <returns></returns>
153    /// <throws>NotSupportedException</throws>
154    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
155      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
156    }
157
158    /// <summary>
159    /// Not supported by the LightweightApplicationManager
160    /// </summary>
161    /// <param name="type"></param>
162    /// <param name="plugin"></param>
163    /// <param name="onlyInstantiable"></param>
164    /// <returns></returns>
165    /// <throws>NotSupportedException</throws>
166    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
167      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
168    }
169
170    /// <summary>
171    /// Not supported by the LightweightApplicationManager
172    /// </summary>
173    /// <param name="type"></param>
174    /// <returns></returns>
175    /// <throws>NotSupportedException</throws>
176    public IPluginDescription GetDeclaringPlugin(Type type) {
177      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
178    }
179
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.