WinGetAppDomain.cs (2442B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="WinGetAppDomain.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 #if POWERSHELL_WINDOWS 7 8 namespace Microsoft.WinGet.Client.Cmdlets.Resolver 9 { 10 using System; 11 using System.IO; 12 using System.Reflection; 13 using System.Runtime.InteropServices; 14 15 /// <summary> 16 /// Resolver for assemblies. 17 /// </summary> 18 internal static class WinGetAppDomain 19 { 20 private static readonly string SharedDependencyPath; 21 private static readonly string SharedArchDependencyPath; 22 private static readonly string DirectDependencyPath; 23 24 static WinGetAppDomain() 25 { 26 var self = typeof(WinGetAppDomain).Assembly; 27 SharedDependencyPath = Path.Combine( 28 Path.GetDirectoryName(self.Location), 29 "SharedDependencies"); 30 SharedArchDependencyPath = Path.Combine( 31 SharedDependencyPath, 32 RuntimeInformation.ProcessArchitecture.ToString().ToLower()); 33 DirectDependencyPath = Path.Combine( 34 Path.GetDirectoryName(self.Location), 35 "DirectDependencies"); 36 } 37 38 /// <summary> 39 /// Handler to register in the AppDomain. 40 /// </summary> 41 /// <param name="source">Source.</param> 42 /// <param name="args">Event args.</param> 43 /// <returns>The assembly if found.</returns> 44 public static Assembly Handler(object source, ResolveEventArgs args) 45 { 46 string name = $"{new AssemblyName(args.Name).Name}.dll"; 47 string path = Path.Combine(SharedDependencyPath, name); 48 if (File.Exists(path)) 49 { 50 return Assembly.LoadFile(path); 51 } 52 53 path = Path.Combine(SharedArchDependencyPath, name); 54 if (File.Exists(path)) 55 { 56 return Assembly.LoadFile(path); 57 } 58 59 path = Path.Combine(DirectDependencyPath, name); 60 if (File.Exists(path)) 61 { 62 return Assembly.LoadFile(path); 63 } 64 65 return null; 66 } 67 } 68 } 69 #endif