-
Notifications
You must be signed in to change notification settings - Fork 278
/
AssemblyFinder.cs
125 lines (102 loc) · 3.68 KB
/
AssemblyFinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace StructureMap.Graph
{
public static class AssemblyFinder
{
public static IEnumerable<Assembly> FindAssemblies(Action<string> logFailure, bool includeExeFiles, Func<string, bool> pathFilter = null)
{
#if NET45
var assemblyPath = AppDomain.CurrentDomain.BaseDirectory;
var binPath = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
if (string.IsNullOrEmpty(binPath))
{
return FindAssemblies(assemblyPath, logFailure, includeExeFiles, pathFilter);
}
if (Path.IsPathRooted(binPath))
{
return FindAssemblies(binPath, logFailure, includeExeFiles, pathFilter);
}
var binPaths = binPath.Split(';');
return binPaths.SelectMany(bin =>
{
var path = Path.Combine(assemblyPath, bin);
return FindAssemblies(path, logFailure, includeExeFiles, pathFilter);
});
#else
string path;
try {
path = AppContext.BaseDirectory;
}
catch (Exception) {
path = System.IO.Directory.GetCurrentDirectory();
}
return FindAssemblies(path, logFailure, includeExeFiles, pathFilter);
#endif
}
public static IEnumerable<Assembly> FindAssemblies(string assemblyPath, Action<string> logFailure, bool includeExeFiles, Func<string, bool> pathFilter = null)
{
var dllFiles = Directory.EnumerateFiles(assemblyPath, "*.dll", SearchOption.AllDirectories);
var files = dllFiles;
if (includeExeFiles)
{
var exeFiles = Directory.EnumerateFiles(assemblyPath, "*.exe", SearchOption.AllDirectories);
files = dllFiles.Concat(exeFiles);
}
foreach (var file in files)
{
var name = Path.GetFileNameWithoutExtension(file);
Assembly assembly = null;
if (pathFilter != null)
{
if (!pathFilter(file))
continue;
}
try
{
#if NET45 || NETSTANDARD2_0
assembly = AppDomain.CurrentDomain.Load(name);
#endif
#if NETSTANDARD1_3
assembly = Assembly.Load(new AssemblyName(name));
#endif
#if NETSTANDARD1_5
assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(name));
#endif
}
catch (Exception)
{
try
{
#if NET45 || NETSTANDARD2_0
assembly = Assembly.LoadFrom(file);
#endif
#if NETSTANDARD1_5
assembly = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(file);
#endif
}
catch (Exception)
{
logFailure(file);
}
}
if (assembly != null)
{
yield return assembly;
}
}
}
public static IEnumerable<Assembly> FindAssemblies(Func<Assembly, bool> filter,
Func<string, bool> pathFilter = null, bool includeExeFiles = false)
{
if (filter == null)
{
filter = a => true;
}
return FindAssemblies(file => { }, includeExeFiles: includeExeFiles, pathFilter: pathFilter).Where(filter);
}
}
}