-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAssemblyLoadContext.cs
More file actions
36 lines (27 loc) · 1.48 KB
/
TestAssemblyLoadContext.cs
File metadata and controls
36 lines (27 loc) · 1.48 KB
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
using System.Reflection;
using System.Runtime.Loader;
using NLog;
namespace IsolatedTests;
internal class TestAssemblyLoadContext : AssemblyLoadContext {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly AssemblyDependencyResolver _resolver;
internal TestAssemblyLoadContext(string baseAssemblyPath, bool isCollectible) : base(isCollectible) {
_resolver = new AssemblyDependencyResolver(baseAssemblyPath);
}
// Resolver of the locations of the assemblies that are dependencies of the
// main plugin assembly.
// The Load method override causes all the dependencies present in the plugin's binary directory to get loaded
// into the HostAssemblyLoadContext together with the plugin assembly itself.
// NOTE: The Interface assembly must not be present in the plugin's binary directory, otherwise we would
// end up with the assembly being loaded twice. Once in the default context and once in the HostAssemblyLoadContext.
// The types present on the host and plugin side would then not match even though they would have the same names.
protected override Assembly Load(AssemblyName assemblyName)
{
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath == null) {
return null;
}
Logger.Info($"Loading assembly {assemblyPath} into the TestAssemblyLoadContext");
return LoadFromAssemblyPath(assemblyPath);
}
}