Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/PluginLoader.cs @ 17460

Last change on this file since 17460 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 2.5 KB
RevLine 
[6545]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6545]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
[4490]22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using HeuristicLab.PluginInfrastructure;
28
[9885]29namespace HeuristicLab.Tests {
[7915]30  public static class PluginLoader {
[5407]31    public const string ExecutableExtension = ".exe";
[4490]32    public const string AssemblyExtension = ".dll";
[5151]33    public const string TestAccessorAssemblyExtension = "_Accessor.dll";
[5272]34    public const string TestAssemblyExtension = ".Tests.dll";
[5306]35    public static List<Assembly> Assemblies;
[4487]36
[4490]37    static PluginLoader() {
[9885]38      foreach (string path in Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory).Where(IsRelevantAssemblyPath)) {
[5407]39        try {
40          Assembly.LoadFrom(path);
[9885]41        } catch (BadImageFormatException) { }
[5407]42      }
[6901]43      // test relevant path again to exclude previously loaded assemblies
44      Assemblies = AppDomain.CurrentDomain.GetAssemblies()
45        .Where(asm => IsRelevantAssemblyPath(asm.Location))
46        .ToList();
[4490]47    }
[4487]48
[5407]49    private static bool IsRelevantAssemblyPath(string path) {
50      bool valid = true;
[6901]51      valid = valid && path.EndsWith(ExecutableExtension, StringComparison.OrdinalIgnoreCase) || path.EndsWith(AssemblyExtension, StringComparison.OrdinalIgnoreCase);
[5407]52      valid = valid && !path.EndsWith(TestAccessorAssemblyExtension, StringComparison.OrdinalIgnoreCase) && !path.EndsWith(TestAssemblyExtension, StringComparison.OrdinalIgnoreCase);
53      return valid;
54    }
55
[5306]56    public static bool IsPluginAssembly(Assembly assembly) {
[5407]57      return assembly.GetExportedTypes().Any(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
[4490]58    }
[4486]59  }
60}
Note: See TracBrowser for help on using the repository browser.