1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using HeuristicLab.PluginInfrastructure;
|
---|
28 |
|
---|
29 | namespace HeuristicLab_33.Tests {
|
---|
30 | public static class PluginLoader {
|
---|
31 | public const string ExecutableExtension = ".exe";
|
---|
32 | public const string AssemblyExtension = ".dll";
|
---|
33 | public const string TestAccessorAssemblyExtension = "_Accessor.dll";
|
---|
34 | public const string TestAssemblyExtension = ".Tests.dll";
|
---|
35 | public static List<Assembly> Assemblies;
|
---|
36 |
|
---|
37 | static PluginLoader() {
|
---|
38 | //EnvDTE.DTE dte = (EnvDTE.DTE)Marshal.GetActiveObject("VisualStudio.DTE");
|
---|
39 | //string solutionDir = System.IO.Path.GetDirectoryName(_applicationObject.Solution.FullName);
|
---|
40 | foreach (string path in Directory.EnumerateFiles(Environment.CurrentDirectory).Where(IsRelevantAssemblyPath)) {
|
---|
41 | try {
|
---|
42 | Assembly.LoadFrom(path);
|
---|
43 | }
|
---|
44 | catch (BadImageFormatException) { }
|
---|
45 | }
|
---|
46 | // test relevant path again to exclude previously loaded assemblies
|
---|
47 | Assemblies = AppDomain.CurrentDomain.GetAssemblies()
|
---|
48 | .Where(asm => IsRelevantAssemblyPath(asm.Location))
|
---|
49 | .ToList();
|
---|
50 | }
|
---|
51 |
|
---|
52 | private static bool IsRelevantAssemblyPath(string path) {
|
---|
53 | bool valid = true;
|
---|
54 | valid = valid && path.EndsWith(ExecutableExtension, StringComparison.OrdinalIgnoreCase) || path.EndsWith(AssemblyExtension, StringComparison.OrdinalIgnoreCase);
|
---|
55 | valid = valid && !path.EndsWith(TestAccessorAssemblyExtension, StringComparison.OrdinalIgnoreCase) && !path.EndsWith(TestAssemblyExtension, StringComparison.OrdinalIgnoreCase);
|
---|
56 | return valid;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static bool IsPluginAssembly(Assembly assembly) {
|
---|
60 | return assembly.GetExportedTypes().Any(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|