Files
Dongbo Wang 77e256abcb Update build to use net462 (#3285)
net461 is reaching end of support on 4/26/2022.
2022-04-20 16:01:19 -07:00

38 lines
1.2 KiB
C#

using System;
using System.IO;
using System.Management.Automation;
using System.Reflection;
namespace Microsoft.PowerShell.PSReadLine
{
public class OnModuleImportAndRemove : IModuleAssemblyInitializer, IModuleAssemblyCleanup
{
public void OnImport()
{
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
}
public void OnRemove(PSModuleInfo module)
{
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
}
/// <summary>
/// Load the correct 'Polyfiller' assembly based on the runtime.
/// </summary>
private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
{
if (args.Name != "Microsoft.PowerShell.PSReadLine.Polyfiller, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
{
return null;
}
string root = Path.GetDirectoryName(typeof(OnModuleImportAndRemove).Assembly.Location);
string subd = (Environment.Version.Major >= 6) ? "net6plus" : "net462";
string path = Path.Combine(root, subd, "Microsoft.PowerShell.PSReadLine.Polyfiller.dll");
return Assembly.LoadFrom(path);
}
}
}