-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCustomALC.cs
59 lines (50 loc) · 1.72 KB
/
CustomALC.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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Management.Automation;
namespace CustomAlc
{
public class MyAlc : AssemblyLoadContext
{
private static MyAlc s_myAlc = new(
Path.Combine(
Path.GetDirectoryName(typeof(MyAlc).Assembly.Location),
"Dependencies"));
private string dependencyFolder;
private MyAlc(string folder)
: base("MyCustomALC", isCollectible: false)
{
dependencyFolder = folder;
}
protected override Assembly Load(AssemblyName assemblyName)
{
string path = Path.Combine(dependencyFolder, assemblyName.Name) + ".dll";
if (File.Exists(path))
{
return LoadFromAssemblyPath(path);
}
return null;
}
internal static Assembly ResolvingHandler(AssemblyLoadContext context, AssemblyName name)
{
if (name.FullName.Equals("SharedDependency, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine($"<*** Fall in 'ResolvingHandler': SharedDependency, Version=1.0.0.0 -- Loaded! ***>");
return s_myAlc.LoadFromAssemblyName(name);
}
return null;
}
}
public class Init : IModuleAssemblyInitializer, IModuleAssemblyCleanup
{
public void OnImport()
{
AssemblyLoadContext.Default.Resolving += MyAlc.ResolvingHandler;
}
public void OnRemove(PSModuleInfo module)
{
AssemblyLoadContext.Default.Resolving -= MyAlc.ResolvingHandler;
}
}
}