[3247] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15973] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3247] | 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;
|
---|
[4068] | 24 | using System.Linq;
|
---|
[3247] | 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 {
|
---|
[4482] | 35 | internal LightweightApplicationManager() {
|
---|
| 36 | AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
|
---|
| 37 | }
|
---|
[3247] | 38 |
|
---|
[16993] | 39 | Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) => null;
|
---|
[4482] | 40 |
|
---|
| 41 |
|
---|
[3247] | 42 | #region IApplicationManager Members
|
---|
| 43 | /// <summary>
|
---|
| 44 | /// Gets an empty list of plugins. (LightweightApplicationManager doesn't support plugin discovery)
|
---|
| 45 | /// </summary>
|
---|
[16993] | 46 | public IEnumerable<IPluginDescription> Plugins => new IPluginDescription[0];
|
---|
[3247] | 47 |
|
---|
| 48 | /// <summary>
|
---|
| 49 | /// Gets an empty list of applications. (LightweightApplicationManager doesn't support application discovery)
|
---|
| 50 | /// </summary>
|
---|
[16993] | 51 | public IEnumerable<IApplicationDescription> Applications => new IApplicationDescription[0];
|
---|
[3247] | 52 |
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// Creates an instance of all types that are subtypes or the same type of the specified type
|
---|
| 55 | /// </summary>
|
---|
| 56 | /// <typeparam name="T">Most general type.</typeparam>
|
---|
| 57 | /// <returns>Enumerable of the created instances.</returns>
|
---|
[16993] | 58 | public IEnumerable<T> GetInstances<T>() where T : class => GetInstances(typeof(T)).Cast<T>();
|
---|
[3247] | 59 |
|
---|
| 60 | /// <summary>
|
---|
| 61 | /// Creates an instance of all types that are subtypes or the same type of the specified type
|
---|
| 62 | /// </summary>
|
---|
[16993] | 63 | /// <typeparam name="T">Most general type.</typeparam>
|
---|
| 64 | /// <param name="args">Constructor arguments.</param>
|
---|
| 65 | /// <returns>Enumerable of the created instances.</returns>
|
---|
| 66 | public IEnumerable<T> GetInstances<T>(params object[] args) where T : class => GetInstances(typeof(T), args).Cast<T>();
|
---|
| 67 |
|
---|
| 68 | /// <summary>
|
---|
| 69 | /// Creates an instance of all types that are subtypes or the same type of the specified type
|
---|
| 70 | /// </summary>
|
---|
[3247] | 71 | /// <param name="type">Most general type.</param>
|
---|
| 72 | /// <returns>Enumerable of the created instances.</returns>
|
---|
[16993] | 73 | public IEnumerable<object> GetInstances(Type type) => GetInstances(type, null);
|
---|
| 74 |
|
---|
| 75 | /// <summary>
|
---|
| 76 | /// Creates an instance of all types that are subtypes or the same type of the specified type
|
---|
| 77 | /// </summary>
|
---|
| 78 | /// <param name="type">Most general type.</param>
|
---|
| 79 | /// <param name="args">Constructor arguments.</param>
|
---|
| 80 | /// <returns>Enumerable of the created instances.</returns>
|
---|
| 81 | public IEnumerable<object> GetInstances(Type type, params object[] args) {
|
---|
[5131] | 82 | List<object> instances = new List<object>();
|
---|
[7111] | 83 | foreach (Type t in GetTypes(type)) {
|
---|
[5131] | 84 | object instance = null;
|
---|
[16993] | 85 | try { instance = Activator.CreateInstance(t, args); } catch { }
|
---|
[5131] | 86 | if (instance != null) instances.Add(instance);
|
---|
| 87 | }
|
---|
| 88 | return instances;
|
---|
[3247] | 89 | }
|
---|
| 90 |
|
---|
[16993] | 91 | /// <summary>
|
---|
| 92 | /// Discovers a specific type by its name.
|
---|
| 93 | /// </summary>
|
---|
| 94 | /// <param name="typeName">Full name of the type.</param>
|
---|
| 95 | /// <returns>A type or null, if nothing was found.</returns>
|
---|
[16984] | 96 | public Type GetType(string typeName) {
|
---|
| 97 | foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) {
|
---|
| 98 | Type t = asm.GetType(typeName);
|
---|
| 99 | if (t != null) return t;
|
---|
| 100 | }
|
---|
| 101 | return null;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[3247] | 104 | /// <summary>
|
---|
[5850] | 105 | /// Finds all instantiable types that are subtypes or equal to the specified types.
|
---|
[3247] | 106 | /// </summary>
|
---|
[5850] | 107 | /// <param name="types">Most general types for which to find matching types.</param>
|
---|
[3247] | 108 | /// <remarks>Return only types that are instantiable
|
---|
| 109 | /// (interfaces, abstract classes... are not returned)</remarks>
|
---|
[7111] | 110 | /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
|
---|
[3247] | 111 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
[7111] | 112 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
|
---|
| 113 | IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable, includeGenericTypeDefinitions);
|
---|
[5850] | 114 | foreach (Type type in types.Skip(1)) {
|
---|
[7111] | 115 | IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
|
---|
[5903] | 116 | if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
|
---|
[5850] | 117 | else result = result.Union(discoveredTypes);
|
---|
| 118 | }
|
---|
| 119 | return result;
|
---|
[3247] | 120 | }
|
---|
| 121 |
|
---|
| 122 | /// <summary>
|
---|
| 123 | /// Finds all types that are subtypes or equal to the specified type.
|
---|
| 124 | /// </summary>
|
---|
| 125 | /// <param name="type">Most general type for which to find matching types.</param>
|
---|
| 126 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
| 127 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
[7111] | 128 | /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
|
---|
[3247] | 129 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
[7111] | 130 | public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
|
---|
[3247] | 131 | return from asm in AppDomain.CurrentDomain.GetAssemblies()
|
---|
[7111] | 132 | from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
|
---|
[3247] | 133 | select t;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /// <summary>
|
---|
| 137 | /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
|
---|
| 138 | /// </summary>
|
---|
| 139 | /// <param name="type">Most general type we want to find.</param>
|
---|
| 140 | /// <param name="assembly">Assembly that should be searched for types.</param>
|
---|
| 141 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
| 142 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
| 143 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
[11771] | 144 | public IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
|
---|
[4482] | 145 | try {
|
---|
[7662] | 146 | // necessary to make sure the exception is immediately thrown
|
---|
| 147 | // instead of later when the enumerable is iterated?
|
---|
[16984] | 148 |
|
---|
[4482] | 149 | var assemblyTypes = assembly.GetTypes();
|
---|
| 150 |
|
---|
[8571] | 151 | var matchingTypes = from assemblyType in assembly.GetTypes()
|
---|
| 152 | let t = assemblyType.BuildType(type)
|
---|
| 153 | where t != null
|
---|
| 154 | where t.IsSubTypeOf(type)
|
---|
| 155 | where !t.IsNonDiscoverableType()
|
---|
| 156 | where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
|
---|
| 157 | where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
|
---|
| 158 | select t;
|
---|
[7069] | 159 |
|
---|
[8571] | 160 | return matchingTypes;
|
---|
[16984] | 161 | } catch (TypeLoadException) {
|
---|
[4482] | 162 | return Enumerable.Empty<Type>();
|
---|
[16984] | 163 | } catch (ReflectionTypeLoadException) {
|
---|
[4482] | 164 | return Enumerable.Empty<Type>();
|
---|
| 165 | }
|
---|
[3247] | 166 | }
|
---|
| 167 |
|
---|
| 168 | /// <summary>
|
---|
[11877] | 169 | /// Discovers all types implementing or inheriting all or any type in <paramref name="types"/> (directly and indirectly) that are declared in the assembly <paramref name="assembly"/>.
|
---|
[11771] | 170 | /// </summary>
|
---|
| 171 | /// <param name="types">The types to discover.</param>
|
---|
| 172 | /// <param name="assembly">The declaring assembly.</param>
|
---|
| 173 | /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
|
---|
| 174 | /// /// <param name="assignableToAllTypes">Specifies if discovered types must implement or inherit all given <paramref name="types"/>.</param>
|
---|
| 175 | /// <returns>An enumerable of discovered types.</returns>
|
---|
| 176 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
|
---|
| 177 | IEnumerable<Type> result = GetTypes(types.First(), assembly, onlyInstantiable, includeGenericTypeDefinitions);
|
---|
| 178 | foreach (Type type in types.Skip(1)) {
|
---|
| 179 | IEnumerable<Type> discoveredTypes = GetTypes(type, assembly, onlyInstantiable, includeGenericTypeDefinitions);
|
---|
| 180 | if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
|
---|
| 181 | else result = result.Union(discoveredTypes);
|
---|
| 182 | }
|
---|
| 183 | return result;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /// <summary>
|
---|
[3247] | 187 | /// Not supported by the LightweightApplicationManager
|
---|
| 188 | /// </summary>
|
---|
| 189 | /// <param name="type"></param>
|
---|
| 190 | /// <param name="plugin"></param>
|
---|
| 191 | /// <returns></returns>
|
---|
| 192 | /// <throws>NotSupportedException</throws>
|
---|
| 193 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
|
---|
| 194 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /// <summary>
|
---|
| 198 | /// Not supported by the LightweightApplicationManager
|
---|
| 199 | /// </summary>
|
---|
| 200 | /// <param name="type"></param>
|
---|
| 201 | /// <param name="plugin"></param>
|
---|
| 202 | /// <param name="onlyInstantiable"></param>
|
---|
[7111] | 203 | /// <param name="includeGenericTypeDefinitions"></param>
|
---|
[3247] | 204 | /// <returns></returns>
|
---|
| 205 | /// <throws>NotSupportedException</throws>
|
---|
[7111] | 206 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
|
---|
[3247] | 207 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /// <summary>
|
---|
| 211 | /// Not supported by the LightweightApplicationManager
|
---|
| 212 | /// </summary>
|
---|
| 213 | /// <param name="type"></param>
|
---|
[5850] | 214 | /// <param name="plugin"></param>
|
---|
| 215 | /// <param name="onlyInstantiable"></param>
|
---|
[7111] | 216 | /// <param name="includeGenericTypeDefinitions"></param>
|
---|
[3247] | 217 | /// <returns></returns>
|
---|
| 218 | /// <throws>NotSupportedException</throws>
|
---|
[7111] | 219 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
|
---|
[5850] | 220 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /// <summary>
|
---|
| 224 | /// Not supported by the LightweightApplicationManager
|
---|
| 225 | /// </summary>
|
---|
| 226 | /// <param name="type"></param>
|
---|
| 227 | /// <returns></returns>
|
---|
| 228 | /// <throws>NotSupportedException</throws>
|
---|
[3247] | 229 | public IPluginDescription GetDeclaringPlugin(Type type) {
|
---|
| 230 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 231 | }
|
---|
| 232 | #endregion
|
---|
| 233 | }
|
---|
| 234 | }
|
---|