Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4041 was 3831, checked in by swagner, 15 years ago

Restructured repository in preparation of the HeuristicLab 3.3.0 release (#1000)

File size: 6.9 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.Security.Policy;
25using System.Reflection;
26using System.Diagnostics;
27using System.Security.Permissions;
28using System.Security;
29using System.Linq;
30using HeuristicLab.PluginInfrastructure.Manager;
31using System.IO;
32
33namespace HeuristicLab.PluginInfrastructure {
34
35  /// <summary>
36  /// Lightweight application manager is set as the application manager as long as the plugin infrastructure is uninitialized.
37  /// The list of plugins and applications is empty. The default application manager is necessary to provide the type discovery
38  /// functionality in unit tests.
39  /// </summary>
40  internal sealed class LightweightApplicationManager : IApplicationManager {
41
42    #region IApplicationManager Members
43    /// <summary>
44    /// Gets an empty list of plugins. (LightweightApplicationManager doesn't support plugin discovery)
45    /// </summary>
46    public IEnumerable<IPluginDescription> Plugins {
47      get { return new IPluginDescription[0]; }
48    }
49
50    /// <summary>
51    /// Gets an empty list of applications. (LightweightApplicationManager doesn't support application discovery)
52    /// </summary>
53    public IEnumerable<IApplicationDescription> Applications {
54      get { return new IApplicationDescription[0]; }
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>Enumerable of the created instances.</returns>
62    public IEnumerable<T> GetInstances<T>() where T : class {
63      return GetInstances(typeof(T)).Cast<T>();
64    }
65
66    /// <summary>
67    /// Creates an instance of all types that are subtypes or the same type of the specified type
68    /// </summary>
69    /// <param name="type">Most general type.</param>
70    /// <returns>Enumerable of the created instances.</returns>
71    public IEnumerable<object> GetInstances(Type type) {
72      return from t in GetTypes(type, true)
73             select Activator.CreateInstance(t);
74    }
75
76
77    /// <summary>
78    /// Finds all instantiable types that are subtypes or equal to the specified type.
79    /// </summary>
80    /// <param name="type">Most general type for which to find matching types.</param>
81    /// <remarks>Return only types that are instantiable
82    /// (interfaces, abstract classes... are not returned)</remarks>
83    /// <returns>Enumerable of the discovered types.</returns>
84    public IEnumerable<Type> GetTypes(Type type) {
85      return GetTypes(type, true);
86    }
87
88    /// <summary>
89    /// Finds all types that are subtypes or equal to the specified type.
90    /// </summary>
91    /// <param name="type">Most general type for which to find matching types.</param>
92    /// <param name="onlyInstantiable">Return only types that are instantiable
93    /// (interfaces, abstract classes... are not returned)</param>
94    /// <returns>Enumerable of the discovered types.</returns>
95    public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
96      return from asm in AppDomain.CurrentDomain.GetAssemblies()
97             from t in GetTypes(type, asm, onlyInstantiable)
98             select t;
99    }
100
101    /// <summary>
102    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
103    /// </summary>
104    /// <param name="type">Most general type we want to find.</param>
105    /// <param name="assembly">Assembly that should be searched for types.</param>
106    /// <param name="onlyInstantiable">Return only types that are instantiable
107    /// (interfaces, abstract classes...  are not returned)</param>
108    /// <returns>Enumerable of the discovered types.</returns>
109    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
110      return from t in assembly.GetTypes()
111             where CheckTypeCompatibility(type, t)
112             where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
113             select BuildType(t, type);
114    }
115
116    private static bool CheckTypeCompatibility(Type type, Type other) {
117      if (type.IsAssignableFrom(other))
118        return true;
119      if (type.IsGenericType && other.IsGenericType) {
120        try {
121          if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
122            return true;
123        }
124        catch (Exception) { }
125      }
126      return false;
127    }
128    private static Type BuildType(Type type, Type protoType) {
129      if (type.IsGenericType && protoType.IsGenericType)
130        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
131      else
132        return type;
133    }
134
135    /// <summary>
136    /// Not supported by the LightweightApplicationManager
137    /// </summary>
138    /// <param name="type"></param>
139    /// <param name="plugin"></param>
140    /// <returns></returns>
141    /// <throws>NotSupportedException</throws>
142    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
143      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
144    }
145
146    /// <summary>
147    /// Not supported by the LightweightApplicationManager
148    /// </summary>
149    /// <param name="type"></param>
150    /// <param name="plugin"></param>
151    /// <param name="onlyInstantiable"></param>
152    /// <returns></returns>
153    /// <throws>NotSupportedException</throws>
154    public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
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    /// <returns></returns>
163    /// <throws>NotSupportedException</throws>
164    public IPluginDescription GetDeclaringPlugin(Type type) {
165      throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
166    }
167
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.