[3247] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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 |
|
---|
[4482] | 39 | Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
|
---|
| 40 | return null;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
[3247] | 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) {
|
---|
[5131] | 74 | List<object> instances = new List<object>();
|
---|
[7111] | 75 | foreach (Type t in GetTypes(type)) {
|
---|
[5131] | 76 | object instance = null;
|
---|
[14927] | 77 | try { instance = Activator.CreateInstance(t); } catch { }
|
---|
[5131] | 78 | if (instance != null) instances.Add(instance);
|
---|
| 79 | }
|
---|
| 80 | return instances;
|
---|
[3247] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /// <summary>
|
---|
[5850] | 84 | /// Finds all instantiable types that are subtypes or equal to the specified types.
|
---|
[3247] | 85 | /// </summary>
|
---|
[5850] | 86 | /// <param name="types">Most general types for which to find matching types.</param>
|
---|
[3247] | 87 | /// <remarks>Return only types that are instantiable
|
---|
| 88 | /// (interfaces, abstract classes... are not returned)</remarks>
|
---|
[7111] | 89 | /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
|
---|
[3247] | 90 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
[7111] | 91 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
|
---|
| 92 | IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable, includeGenericTypeDefinitions);
|
---|
[5850] | 93 | foreach (Type type in types.Skip(1)) {
|
---|
[7111] | 94 | IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
|
---|
[5903] | 95 | if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
|
---|
[5850] | 96 | else result = result.Union(discoveredTypes);
|
---|
| 97 | }
|
---|
| 98 | return result;
|
---|
[3247] | 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>
|
---|
[7111] | 107 | /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
|
---|
[3247] | 108 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
[7111] | 109 | public IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
|
---|
[3247] | 110 | return from asm in AppDomain.CurrentDomain.GetAssemblies()
|
---|
[7111] | 111 | from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
|
---|
[3247] | 112 | select t;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /// <summary>
|
---|
| 116 | /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
|
---|
| 117 | /// </summary>
|
---|
| 118 | /// <param name="type">Most general type we want to find.</param>
|
---|
| 119 | /// <param name="assembly">Assembly that should be searched for types.</param>
|
---|
| 120 | /// <param name="onlyInstantiable">Return only types that are instantiable
|
---|
| 121 | /// (interfaces, abstract classes... are not returned)</param>
|
---|
| 122 | /// <returns>Enumerable of the discovered types.</returns>
|
---|
[11771] | 123 | public IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
|
---|
[4482] | 124 | try {
|
---|
[7662] | 125 | // necessary to make sure the exception is immediately thrown
|
---|
| 126 | // instead of later when the enumerable is iterated?
|
---|
[4482] | 127 | var assemblyTypes = assembly.GetTypes();
|
---|
| 128 |
|
---|
[8571] | 129 | var matchingTypes = from assemblyType in assembly.GetTypes()
|
---|
| 130 | let t = assemblyType.BuildType(type)
|
---|
| 131 | where t != null
|
---|
| 132 | where t.IsSubTypeOf(type)
|
---|
| 133 | where !t.IsNonDiscoverableType()
|
---|
| 134 | where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
|
---|
| 135 | where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
|
---|
| 136 | select t;
|
---|
[7069] | 137 |
|
---|
[8571] | 138 | return matchingTypes;
|
---|
[14927] | 139 | } catch (TypeLoadException) {
|
---|
[4482] | 140 | return Enumerable.Empty<Type>();
|
---|
[14927] | 141 | } catch (ReflectionTypeLoadException) {
|
---|
[4482] | 142 | return Enumerable.Empty<Type>();
|
---|
| 143 | }
|
---|
[3247] | 144 | }
|
---|
| 145 |
|
---|
| 146 | /// <summary>
|
---|
[11877] | 147 | /// 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] | 148 | /// </summary>
|
---|
| 149 | /// <param name="types">The types to discover.</param>
|
---|
| 150 | /// <param name="assembly">The declaring assembly.</param>
|
---|
| 151 | /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
|
---|
| 152 | /// /// <param name="assignableToAllTypes">Specifies if discovered types must implement or inherit all given <paramref name="types"/>.</param>
|
---|
| 153 | /// <returns>An enumerable of discovered types.</returns>
|
---|
| 154 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
|
---|
| 155 | IEnumerable<Type> result = GetTypes(types.First(), assembly, onlyInstantiable, includeGenericTypeDefinitions);
|
---|
| 156 | foreach (Type type in types.Skip(1)) {
|
---|
| 157 | IEnumerable<Type> discoveredTypes = GetTypes(type, assembly, onlyInstantiable, includeGenericTypeDefinitions);
|
---|
| 158 | if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
|
---|
| 159 | else result = result.Union(discoveredTypes);
|
---|
| 160 | }
|
---|
| 161 | return result;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /// <summary>
|
---|
[3247] | 165 | /// Not supported by the LightweightApplicationManager
|
---|
| 166 | /// </summary>
|
---|
| 167 | /// <param name="type"></param>
|
---|
| 168 | /// <param name="plugin"></param>
|
---|
| 169 | /// <returns></returns>
|
---|
| 170 | /// <throws>NotSupportedException</throws>
|
---|
| 171 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin) {
|
---|
| 172 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /// <summary>
|
---|
| 176 | /// Not supported by the LightweightApplicationManager
|
---|
| 177 | /// </summary>
|
---|
| 178 | /// <param name="type"></param>
|
---|
| 179 | /// <param name="plugin"></param>
|
---|
| 180 | /// <param name="onlyInstantiable"></param>
|
---|
[7111] | 181 | /// <param name="includeGenericTypeDefinitions"></param>
|
---|
[3247] | 182 | /// <returns></returns>
|
---|
| 183 | /// <throws>NotSupportedException</throws>
|
---|
[7111] | 184 | public IEnumerable<Type> GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false) {
|
---|
[3247] | 185 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /// <summary>
|
---|
| 189 | /// Not supported by the LightweightApplicationManager
|
---|
| 190 | /// </summary>
|
---|
| 191 | /// <param name="type"></param>
|
---|
[5850] | 192 | /// <param name="plugin"></param>
|
---|
| 193 | /// <param name="onlyInstantiable"></param>
|
---|
[7111] | 194 | /// <param name="includeGenericTypeDefinitions"></param>
|
---|
[3247] | 195 | /// <returns></returns>
|
---|
| 196 | /// <throws>NotSupportedException</throws>
|
---|
[7111] | 197 | public IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
|
---|
[5850] | 198 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /// <summary>
|
---|
| 202 | /// Not supported by the LightweightApplicationManager
|
---|
| 203 | /// </summary>
|
---|
| 204 | /// <param name="type"></param>
|
---|
| 205 | /// <returns></returns>
|
---|
| 206 | /// <throws>NotSupportedException</throws>
|
---|
[3247] | 207 | public IPluginDescription GetDeclaringPlugin(Type type) {
|
---|
| 208 | throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins.");
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | #endregion
|
---|
| 212 | }
|
---|
| 213 | }
|
---|