Files
PowerShell-PSReadLine/PSReadLine/OnImportAndRemove.cs
T
Dongbo Wang 49604e89ec Update PSReadLine build to target netstandard2.0 (#4584)
By retargeting PSReadLine to `netstandard2.0`, we can simplify the build a lot and produce the same assembly no matter building on Windows or non-Windows.

- Update PSReadLine build to target `netstandard2.0`, including the CI and release pipeline YAML files.
- Update the test to target `net472` and `net6.0`, so that we can run tests with both .NET Framework and .NET
- Rename `Microsoft.PowerShell.PSReadLine2.dll` to `Microsoft.PowerShell.PSReadLine.dll`
- Update and clean up the `README.md` with the up-to-date information
2025-02-28 11:09:08 -08: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" : "netstd";
string path = Path.Combine(root, subd, "Microsoft.PowerShell.PSReadLine.Polyfiller.dll");
return Assembly.LoadFrom(path);
}
}
}