Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab/3.3/Tests/PluginLoader.cs @ 5985

Last change on this file since 5985 was 5407, checked in by swagner, 14 years ago

Adapted PluginLoader to ignore cases in assembly paths, additionally load exe-files and ignore assemblies which cannot be loaded (#1351)

File size: 1.5 KB
RevLine 
[4486]1
[4490]2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Reflection;
7using HeuristicLab.PluginInfrastructure;
8
[4512]9namespace HeuristicLab_33.Tests {
[4486]10  internal static class PluginLoader {
[5407]11    public const string ExecutableExtension = ".exe";
[4490]12    public const string AssemblyExtension = ".dll";
[5151]13    public const string TestAccessorAssemblyExtension = "_Accessor.dll";
[5272]14    public const string TestAssemblyExtension = ".Tests.dll";
[5306]15    public static List<Assembly> Assemblies;
[4487]16
[4490]17    static PluginLoader() {
[5407]18      foreach (string path in Directory.EnumerateFiles(Environment.CurrentDirectory).Where(s => IsRelevantAssemblyPath(s))) {
19        try {
20          Assembly.LoadFrom(path);
21        }
22        catch (BadImageFormatException) { }
23      }
[5306]24      Assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
[4490]25    }
[4487]26
[5407]27    private static bool IsRelevantAssemblyPath(string path) {
28      bool valid = true;
29      valid = valid && (path.EndsWith(ExecutableExtension, StringComparison.OrdinalIgnoreCase) || path.EndsWith(AssemblyExtension, StringComparison.OrdinalIgnoreCase));
30      valid = valid && !path.EndsWith(TestAccessorAssemblyExtension, StringComparison.OrdinalIgnoreCase) && !path.EndsWith(TestAssemblyExtension, StringComparison.OrdinalIgnoreCase);
31      return valid;
32    }
33
[5306]34    public static bool IsPluginAssembly(Assembly assembly) {
[5407]35      return assembly.GetExportedTypes().Any(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
[4490]36    }
[4486]37  }
38}
Note: See TracBrowser for help on using the repository browser.