Skip to content

Commit 887516a

Browse files
authored
Merge pull request #1370 from veleek/native-assembly-resolve
Load unmanaged assemblies the same way as managed assemblies.
2 parents 945090c + 462d9d2 commit 887516a

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/NUnitEngine/nunit.engine.core/Internal/TestAssemblyLoadContext.cs

+41-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if NETCOREAPP3_1_OR_GREATER
44

55
using System.Reflection;
6+
using System.Runtime.InteropServices;
67
using System.Runtime.Loader;
78
using System.IO;
89
using System;
@@ -56,7 +57,7 @@ protected override Assembly Load(AssemblyName name)
5657
if (loadedAssembly != null)
5758
{
5859
log.Info("Assembly {0} ({1}) is loaded using the TestAssembliesResolver", name, GetAssemblyLocationInfo(loadedAssembly));
59-
60+
6061
return loadedAssembly;
6162
}
6263

@@ -79,6 +80,45 @@ protected override Assembly Load(AssemblyName name)
7980
return loadedAssembly;
8081
}
8182

83+
protected override IntPtr LoadUnmanagedDll(string name)
84+
{
85+
log.Debug("Loading {0} unmanaged dll", name);
86+
87+
IntPtr loadedDllHandle = base.LoadUnmanagedDll(name);
88+
if (loadedDllHandle != IntPtr.Zero)
89+
{
90+
log.Info("Unmanaged DLL {0} is loaded using default base.LoadUnmanagedDll()", name);
91+
return loadedDllHandle;
92+
}
93+
94+
string runtimeResolverPath = _runtimeResolver.ResolveUnmanagedDllToPath(name);
95+
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
96+
File.Exists(runtimeResolverPath))
97+
{
98+
loadedDllHandle = LoadUnmanagedDllFromPath(runtimeResolverPath);
99+
}
100+
101+
if (loadedDllHandle != IntPtr.Zero)
102+
{
103+
log.Info("Unmanaged DLL {0} ({1}) is loaded using the deps.json info", name, runtimeResolverPath);
104+
return loadedDllHandle;
105+
}
106+
107+
string unmanagedDllPath = Path.Combine(_basePath, name + ".dll");
108+
if (File.Exists(unmanagedDllPath))
109+
{
110+
loadedDllHandle = LoadUnmanagedDllFromPath(unmanagedDllPath);
111+
}
112+
113+
if (loadedDllHandle != IntPtr.Zero)
114+
{
115+
log.Info("Unmanaged DLL {0} ({1}) is loaded using base path", name, unmanagedDllPath);
116+
return loadedDllHandle;
117+
}
118+
119+
return IntPtr.Zero;
120+
}
121+
82122
private static string GetAssemblyLocationInfo(Assembly assembly)
83123
{
84124
if (assembly.IsDynamic)

0 commit comments

Comments
 (0)