Last change
on this file since 6305 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
|
Line | |
---|
1 |
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.IO;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Reflection;
|
---|
7 | using HeuristicLab.PluginInfrastructure;
|
---|
8 |
|
---|
9 | namespace HeuristicLab_33.Tests {
|
---|
10 | internal static class PluginLoader {
|
---|
11 | public const string ExecutableExtension = ".exe";
|
---|
12 | public const string AssemblyExtension = ".dll";
|
---|
13 | public const string TestAccessorAssemblyExtension = "_Accessor.dll";
|
---|
14 | public const string TestAssemblyExtension = ".Tests.dll";
|
---|
15 | public static List<Assembly> Assemblies;
|
---|
16 |
|
---|
17 | static PluginLoader() {
|
---|
18 | foreach (string path in Directory.EnumerateFiles(Environment.CurrentDirectory).Where(s => IsRelevantAssemblyPath(s))) {
|
---|
19 | try {
|
---|
20 | Assembly.LoadFrom(path);
|
---|
21 | }
|
---|
22 | catch (BadImageFormatException) { }
|
---|
23 | }
|
---|
24 | Assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
|
---|
25 | }
|
---|
26 |
|
---|
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 |
|
---|
34 | public static bool IsPluginAssembly(Assembly assembly) {
|
---|
35 | return assembly.GetExportedTypes().Any(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.