-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathTestAssemblyLoadContext.cs
50 lines (41 loc) · 1.79 KB
/
TestAssemblyLoadContext.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
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
#if NETCOREAPP3_1_OR_GREATER
using System.Reflection;
using System.Runtime.Loader;
using System.IO;
using System;
using System.Linq;
namespace NUnit.Engine.Internal
{
internal sealed class TestAssemblyLoadContext : AssemblyLoadContext
{
private readonly string _testAssemblyPath;
private readonly string _basePath;
private readonly TestAssemblyResolver _resolver;
public TestAssemblyLoadContext(string testAssemblyPath)
{
_testAssemblyPath = testAssemblyPath;
_resolver = new TestAssemblyResolver(this, testAssemblyPath);
_basePath = Path.GetDirectoryName(testAssemblyPath);
}
protected override Assembly Load(AssemblyName name)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var loadedAssembly = assemblies.FirstOrDefault(x => x.GetName().Name == name.Name);
if (loadedAssembly != null)
loadedAssembly = base.Load(name);
if (loadedAssembly == null)
{
// Load assemblies that are dependencies, and in the same folder as the test assembly,
// but are not fully specified in test assembly deps.json file. This happens when the
// dependencies reference in the csproj file has CopyLocal=false, and for example, the
// reference is a projectReference and has the same output directory as the parent.
string assemblyPath = Path.Combine(_basePath, name.Name + ".dll");
if (File.Exists(assemblyPath))
loadedAssembly = LoadFromAssemblyPath(assemblyPath);
}
return loadedAssembly;
}
}
}
#endif