Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8531 was 8531, checked in by mkommend, 10 years ago

#1919: Refactored calculation of thresholds for SymbolicDiscriminantFunctionClassficationModels and removed the automatic recalculation of thresholds during solution creation.

File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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      List<object> instances = new List<object>();
75      foreach (Type t in GetTypes(type)) {
76        object instance = null;
77        try { instance = Activator.CreateInstance(t); }
78        catch { }
79        if (instance != null) instances.Add(instance);
80      }
81      return instances;
82    }
83
84    /// <summary>
85    /// Finds all instantiable types that are subtypes or equal to the specified types.
86    /// </summary>
87    /// <param name="types">Most general types for which to find matching types.</param>
88    /// <remarks>Return only types that are instantiable
89    /// (interfaces, abstract classes... are not returned)</remarks>
90    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
91    /// <returns>Enumerable of the discovered types.</returns>
92    public IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
93      IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable, includeGenericTypeDefinitions);
94      foreach (Type type in types.Skip(1)) {
95        IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
96        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
97        else result = result.Union(discoveredTypes);
98      }
99      return result;
100    }
101
102    /// <summary>
103    /// Finds all types that are subtypes or equal to the specified type.
104    /// </summary>
105    /// <param name="type">Most general type for which to find matching types.</param>
106    /// <param name="onlyInstantiable">Return only types that are instantiable
107    /// (interfaces, abstract classes... are not returned)</param>
108    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
109    /// <returns>Enumerable of the discovered types.</returns>
110    public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
111      return from asm in AppDomain.CurrentDomain.GetAssemblies()
112             from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
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>
124    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
125      try {
126        // necessary to make sure the exception is immediately thrown
127        // instead of later when the enumerable is iterated?
128        var assemblyTypes = assembly.GetTypes();
129
130        var buildTypes = from t in assembly.GetTypes()
131                         where CheckTypeCompatibility(type, t)
132                         where !IsNonDiscoverableType(t)
133                         where onlyInstantiable == false ||
134                               (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
135                         select BuildType(t, type);
136
137        return from t in buildTypes
138               where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
139               select t;
140      }
141      catch (TypeLoadException) {
142        return Enumerable.Empty<Type>();
143      }
144      catch (ReflectionTypeLoadException) {
145        return Enumerable.Empty<Type>();
146      }
147    }
148
149    private static bool IsNonDiscoverableType(Type t) {
150      return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
151    }
152
153    private static bool CheckTypeCompatibility(Type type, Type other) {
154      if (type.IsAssignableFrom(other))
155        return true;
156      if (type.IsGenericType && other.IsGenericType) {
157        var otherGenericArguments = other.GetGenericArguments();
158        var typeGenericArguments = type.GetGenericArguments();
159
160        //check type arguments count
161        if (otherGenericArguments.Length != typeGenericArguments.Length)
162          return false;
163
164        //check type arguments & constraints
165        int i = 0;
166        foreach (var genericArgument in typeGenericArguments) {
167          if (otherGenericArguments[i].IsGenericParameter) {
168            //check class contraint on generic type parameter
169            if (otherGenericArguments[i].GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
170              if (!genericArgument.IsClass) return false;
171
172            //check default constructor constraint on generic type parameter
173            if (otherGenericArguments[i].GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
174              if (!genericArgument.IsValueType && genericArgument.GetConstructor(Type.EmptyTypes) == null) return false;
175
176            foreach (var constraint in otherGenericArguments[i].GetGenericParameterConstraints())
177              if (!constraint.IsAssignableFrom(genericArgument)) return false;
178          } else if (genericArgument != otherGenericArguments[i]) return false;
179          i++;
180        }
181        //check types
182        try {
183          var otherGenericTypeDefinition = other.GetGenericTypeDefinition();
184          if (type.IsAssignableFrom(otherGenericTypeDefinition.MakeGenericType(typeGenericArguments)))
185            return true;
186        }
187        catch (Exception) { }
188      }
189      return false;
190    }
191    private static Type BuildType(Type type, Type protoType) {
192      if (type.IsGenericType && protoType.IsGenericType)
193        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
194      else
195        return type;
196    }
197
198    /// <summary>
199    /// Not supported by the LightweightApplicationManager
200    /// </summary>
201    /// <param name="type"></param>
202    /// <param name="plugin"></param>
203    /// <returns></returns>
204    /// <throws>NotSupportedException</throws>
205    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
206      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
207    }
208
209    /// <summary>
210    /// Not supported by the LightweightApplicationManager
211    /// </summary>
212    /// <param name="type"></param>
213    /// <param name="plugin"></param>
214    /// <param name="onlyInstantiable"></param>
215    /// <param name="includeGenericTypeDefinitions"></param>
216    /// <returns></returns>
217    /// <throws>NotSupportedException</throws>
218    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
219      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
220    }
221
222    /// <summary>
223    /// Not supported by the LightweightApplicationManager
224    /// </summary>
225    /// <param name="type"></param>
226    /// <param name="plugin"></param>
227    /// <param name="onlyInstantiable"></param>
228    /// <param name="includeGenericTypeDefinitions"></param>
229    /// <returns></returns>
230    /// <throws>NotSupportedException</throws>
231    public IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
232      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
233    }
234
235    /// <summary>
236    /// Not supported by the LightweightApplicationManager
237    /// </summary>
238    /// <param name="type"></param>
239    /// <returns></returns>
240    /// <throws>NotSupportedException</throws>
241    public IPluginDescription GetDeclaringPlugin(Type type) {
242      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
243    }
244
245    #endregion
246  }
247}
Note: See TracBrowser for help on using the repository browser.