1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Reflection;
|
---|
26 |
|
---|
27 | namespace 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, true)) {
|
---|
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 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
91 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable = true, bool assignableToAllTypes = 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 (assignableToAllTypes) result = result.Intersect(discoveredTypes);
|
---|
96 | else result = result.Union(discoveredTypes);
|
---|
97 | }
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /// <summary>
|
---|
102 | /// Finds all types that are subtypes or equal to the specified type.
|
---|
103 | /// </summary>
|
---|
104 | /// <param name="type">Most general type for which to find matching types.</param>
|
---|
105 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
106 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
107 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
108 | public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable = true) {
|
---|
109 | return from asm in AppDomain.CurrentDomain.GetAssemblies()
|
---|
110 | from t in GetTypes(type, asm, onlyInstantiable)
|
---|
111 | select t;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /// <summary>
|
---|
115 | /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
|
---|
116 | /// </summary>
|
---|
117 | /// <param name="type">Most general type we want to find.</param>
|
---|
118 | /// <param name="assembly">Assembly that should be searched for types.</param>
|
---|
119 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
120 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
121 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
122 | private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable = true) {
|
---|
123 | try {
|
---|
124 | var assemblyTypes = assembly.GetTypes();
|
---|
125 |
|
---|
126 | var buildTypes = from t in assemblyTypes
|
---|
127 | where !IsNonDiscoverableType(t)
|
---|
128 | where CheckTypeCompatibility(type, t)
|
---|
129 | where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
|
---|
130 | select BuildType(t, type);
|
---|
131 |
|
---|
132 | return from t in buildTypes
|
---|
133 | where onlyInstantiable == false || !t.IsGenericTypeDefinition
|
---|
134 | select t;
|
---|
135 | }
|
---|
136 | catch (TypeLoadException) {
|
---|
137 | return Enumerable.Empty<Type>();
|
---|
138 | }
|
---|
139 | catch (ReflectionTypeLoadException) {
|
---|
140 | return Enumerable.Empty<Type>();
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | private static bool IsNonDiscoverableType(Type t) {
|
---|
145 | return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
|
---|
146 | }
|
---|
147 |
|
---|
148 | private static bool CheckTypeCompatibility(Type type, Type other) {
|
---|
149 | if (type.IsAssignableFrom(other))
|
---|
150 | return true;
|
---|
151 | if (type.IsGenericType && other.IsGenericType) {
|
---|
152 | try {
|
---|
153 | if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 | catch (Exception) { }
|
---|
157 | }
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | private static Type BuildType(Type type, Type protoType) {
|
---|
161 | if (type.IsGenericType && protoType.IsGenericType)
|
---|
162 | return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
|
---|
163 | else
|
---|
164 | return type;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /// <summary>
|
---|
168 | /// Not supported by the LightweightApplicationManager
|
---|
169 | /// </summary>
|
---|
170 | /// <param name="type"></param>
|
---|
171 | /// <param name="plugin"></param>
|
---|
172 | /// <returns></returns>
|
---|
173 | /// <throws>NotSupportedException</throws>
|
---|
174 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
|
---|
175 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
176 | }
|
---|
177 |
|
---|
178 | /// <summary>
|
---|
179 | /// Not supported by the LightweightApplicationManager
|
---|
180 | /// </summary>
|
---|
181 | /// <param name="type"></param>
|
---|
182 | /// <param name="plugin"></param>
|
---|
183 | /// <param name="onlyInstantiable"></param>
|
---|
184 | /// <returns></returns>
|
---|
185 | /// <throws>NotSupportedException</throws>
|
---|
186 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable = true) {
|
---|
187 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
188 | }
|
---|
189 |
|
---|
190 | /// <summary>
|
---|
191 | /// Not supported by the LightweightApplicationManager
|
---|
192 | /// </summary>
|
---|
193 | /// <param name="type"></param>
|
---|
194 | /// <param name="plugin"></param>
|
---|
195 | /// <param name="onlyInstantiable"></param>
|
---|
196 | /// <returns></returns>
|
---|
197 | /// <throws>NotSupportedException</throws>
|
---|
198 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable = true, bool assignableToAllTypes = true) {
|
---|
199 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
200 | }
|
---|
201 |
|
---|
202 | /// <summary>
|
---|
203 | /// Not supported by the LightweightApplicationManager
|
---|
204 | /// </summary>
|
---|
205 | /// <param name="type"></param>
|
---|
206 | /// <returns></returns>
|
---|
207 | /// <throws>NotSupportedException</throws>
|
---|
208 | public IPluginDescription GetDeclaringPlugin(Type type) {
|
---|
209 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
210 | }
|
---|
211 |
|
---|
212 | #endregion
|
---|
213 | }
|
---|
214 | }
|
---|