ModuleInit.cs (1740B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="ModuleInit.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.WinGet.Resolver 8 { 9 using System; 10 using System.Collections.Generic; 11 using System.Linq; 12 using System.Management.Automation; 13 using System.Runtime.InteropServices; 14 using System.Runtime.Loader; 15 16 /// <summary> 17 /// Initialization class for this module. 18 /// </summary> 19 public class ModuleInit : IModuleAssemblyInitializer, IModuleAssemblyCleanup 20 { 21 private static readonly IEnumerable<Architecture> ValidArchs = new Architecture[] { Architecture.X86, Architecture.X64, Architecture.Arm64 }; 22 23 /// <inheritdoc/> 24 public void OnImport() 25 { 26 var arch = RuntimeInformation.ProcessArchitecture; 27 if (!ValidArchs.Contains(arch)) 28 { 29 throw new NotSupportedException(arch.ToString()); 30 } 31 32 AssemblyLoadContext.Default.Resolving += WinGetAssemblyLoadContext.ResolvingHandler; 33 AssemblyLoadContext.Default.ResolvingUnmanagedDll += WinGetAssemblyLoadContext.ResolvingUnmanagedDllHandler; 34 } 35 36 /// <inheritdoc/> 37 public void OnRemove(PSModuleInfo module) 38 { 39 AssemblyLoadContext.Default.ResolvingUnmanagedDll -= WinGetAssemblyLoadContext.ResolvingUnmanagedDllHandler; 40 AssemblyLoadContext.Default.Resolving -= WinGetAssemblyLoadContext.ResolvingHandler; 41 } 42 } 43 }