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 |
|
---|
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 |
|
---|
36 | #region IApplicationManager Members
|
---|
37 | /// <summary>
|
---|
38 | /// Gets an empty list of plugins. (LightweightApplicationManager doesn't support plugin discovery)
|
---|
39 | /// </summary>
|
---|
40 | public IEnumerable<IPluginDescription> Plugins {
|
---|
41 | get { return new IPluginDescription[0]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Gets an empty list of applications. (LightweightApplicationManager doesn't support application discovery)
|
---|
46 | /// </summary>
|
---|
47 | public IEnumerable<IApplicationDescription> Applications {
|
---|
48 | get { return new IApplicationDescription[0]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Creates an instance of all types that are subtypes or the same type of the specified type
|
---|
53 | /// </summary>
|
---|
54 | /// <typeparam name="T">Most general type.</typeparam>
|
---|
55 | /// <returns>Enumerable of the created instances.</returns>
|
---|
56 | public IEnumerable<T> GetInstances<T>() where T : class {
|
---|
57 | return GetInstances(typeof(T)).Cast<T>();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Creates an instance of all types that are subtypes or the same type of the specified type
|
---|
62 | /// </summary>
|
---|
63 | /// <param name="type">Most general type.</param>
|
---|
64 | /// <returns>Enumerable of the created instances.</returns>
|
---|
65 | public IEnumerable<object> GetInstances(Type type) {
|
---|
66 | return from t in GetTypes(type, true)
|
---|
67 | select Activator.CreateInstance(t);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Finds all instantiable types that are subtypes or equal to the specified type.
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="type">Most general type for which to find matching types.</param>
|
---|
75 | /// <remarks>Return only types that are instantiable
|
---|
76 | /// (interfaces, abstract classes... are not returned)</remarks>
|
---|
77 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
78 | public IEnumerable<Type> GetTypes(Type type) {
|
---|
79 | return GetTypes(type, true);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Finds all types that are subtypes or equal to the specified type.
|
---|
84 | /// </summary>
|
---|
85 | /// <param name="type">Most general type for which to find matching types.</param>
|
---|
86 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
87 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
88 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
89 | public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
|
---|
90 | return from asm in AppDomain.CurrentDomain.GetAssemblies()
|
---|
91 | from t in GetTypes(type, asm, onlyInstantiable)
|
---|
92 | select t;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
96 | /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
|
---|
97 | /// </summary>
|
---|
98 | /// <param name="type">Most general type we want to find.</param>
|
---|
99 | /// <param name="assembly">Assembly that should be searched for types.</param>
|
---|
100 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
101 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
102 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
103 | private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
|
---|
104 | return from t in assembly.GetTypes()
|
---|
105 | where CheckTypeCompatibility(type, t)
|
---|
106 | where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
|
---|
107 | select BuildType(t, type);
|
---|
108 | }
|
---|
109 |
|
---|
110 | private static bool CheckTypeCompatibility(Type type, Type other) {
|
---|
111 | if (type.IsAssignableFrom(other))
|
---|
112 | return true;
|
---|
113 | if (type.IsGenericType && other.IsGenericType) {
|
---|
114 | try {
|
---|
115 | if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | catch (Exception) { }
|
---|
119 | }
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 | private static Type BuildType(Type type, Type protoType) {
|
---|
123 | if (type.IsGenericType && protoType.IsGenericType)
|
---|
124 | return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
|
---|
125 | else
|
---|
126 | return type;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /// <summary>
|
---|
130 | /// Not supported by the LightweightApplicationManager
|
---|
131 | /// </summary>
|
---|
132 | /// <param name="type"></param>
|
---|
133 | /// <param name="plugin"></param>
|
---|
134 | /// <returns></returns>
|
---|
135 | /// <throws>NotSupportedException</throws>
|
---|
136 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
|
---|
137 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
138 | }
|
---|
139 |
|
---|
140 | /// <summary>
|
---|
141 | /// Not supported by the LightweightApplicationManager
|
---|
142 | /// </summary>
|
---|
143 | /// <param name="type"></param>
|
---|
144 | /// <param name="plugin"></param>
|
---|
145 | /// <param name="onlyInstantiable"></param>
|
---|
146 | /// <returns></returns>
|
---|
147 | /// <throws>NotSupportedException</throws>
|
---|
148 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
|
---|
149 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
150 | }
|
---|
151 |
|
---|
152 | /// <summary>
|
---|
153 | /// Not supported by the LightweightApplicationManager
|
---|
154 | /// </summary>
|
---|
155 | /// <param name="type"></param>
|
---|
156 | /// <returns></returns>
|
---|
157 | /// <throws>NotSupportedException</throws>
|
---|
158 | public IPluginDescription GetDeclaringPlugin(Type type) {
|
---|
159 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
160 | }
|
---|
161 |
|
---|
162 | #endregion
|
---|
163 | }
|
---|
164 | }
|
---|