diff options
Diffstat (limited to 'src/ext/Bal/WixToolset.Dnc.Host/DnchostAssemblyLoadContext.cs')
-rw-r--r-- | src/ext/Bal/WixToolset.Dnc.Host/DnchostAssemblyLoadContext.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/ext/Bal/WixToolset.Dnc.Host/DnchostAssemblyLoadContext.cs b/src/ext/Bal/WixToolset.Dnc.Host/DnchostAssemblyLoadContext.cs new file mode 100644 index 00000000..1a383058 --- /dev/null +++ b/src/ext/Bal/WixToolset.Dnc.Host/DnchostAssemblyLoadContext.cs | |||
@@ -0,0 +1,58 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | namespace WixToolset.Dnc.Host | ||
4 | { | ||
5 | using System; | ||
6 | using System.Reflection; | ||
7 | using System.Runtime.Loader; | ||
8 | |||
9 | public sealed class DnchostAssemblyLoadContext : AssemblyLoadContext | ||
10 | { | ||
11 | private readonly AssemblyDependencyResolver resolver; | ||
12 | |||
13 | public DnchostAssemblyLoadContext(string assemblyPath, bool isolateFromDefault) | ||
14 | : base(nameof(DnchostAssemblyLoadContext), isolateFromDefault) | ||
15 | { | ||
16 | this.resolver = new AssemblyDependencyResolver(assemblyPath); | ||
17 | |||
18 | if (!this.IsCollectible) | ||
19 | { | ||
20 | AssemblyLoadContext.Default.Resolving += this.ResolveAssembly; | ||
21 | AssemblyLoadContext.Default.ResolvingUnmanagedDll += this.ResolveUnmanagedDll; | ||
22 | } | ||
23 | } | ||
24 | |||
25 | private Assembly ResolveAssembly(AssemblyLoadContext defaultAlc, AssemblyName assemblyName) | ||
26 | { | ||
27 | var path = this.resolver.ResolveAssemblyToPath(assemblyName); | ||
28 | if (path != null) | ||
29 | { | ||
30 | var targetAlc = this.IsCollectible ? this : defaultAlc; | ||
31 | return targetAlc.LoadFromAssemblyPath(path); | ||
32 | } | ||
33 | |||
34 | return null; | ||
35 | } | ||
36 | |||
37 | private IntPtr ResolveUnmanagedDll(Assembly assembly, string unmanagedDllName) | ||
38 | { | ||
39 | var path = this.resolver.ResolveUnmanagedDllToPath(unmanagedDllName); | ||
40 | if (path != null) | ||
41 | { | ||
42 | return this.LoadUnmanagedDllFromPath(path); | ||
43 | } | ||
44 | |||
45 | return IntPtr.Zero; | ||
46 | } | ||
47 | |||
48 | protected override Assembly Load(AssemblyName assemblyName) | ||
49 | { | ||
50 | return this.ResolveAssembly(AssemblyLoadContext.Default, assemblyName); | ||
51 | } | ||
52 | |||
53 | protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
54 | { | ||
55 | return this.ResolveUnmanagedDll(null, unmanagedDllName); | ||
56 | } | ||
57 | } | ||
58 | } | ||