Files
DllExports/DllExports.MSBuild/NetCoreAssemblyLoadContext.cs
Roman Vaníček 7c8b00ea1c
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Optional assembly signing key
2024-12-19 18:41:00 +01:00

72 lines
1.8 KiB
C#

#if NET
using System;
using System.Linq;
using System.Runtime.Loader;
using System.IO;
using System.Reflection;
namespace DllExports.MSBuild
{
class AssemblyContext : AssemblyLoadContext
{
public AssemblyContext() : base(true)
{
}
}
class NetCoreAssemblyLoadContext : IAssemblyLoadContext
{
private AssemblyContext loader = new AssemblyContext();
private bool unloaded;
public NetCoreAssemblyLoadContext()
{
}
public Assembly LoadAssembly(Stream stream) =>
loader.LoadFromStream(stream);
public void SetDllDirectory(string path)
{
var files = Directory.EnumerateFiles(path, "*.dll").ToArray();
loader.Resolving += (ctx, name) =>
{
foreach (var file in files)
{
if (StringComparer.OrdinalIgnoreCase.Equals(Path.GetFileNameWithoutExtension(file), name.Name))
return ctx.LoadFromAssemblyPath(file);
}
return null;
};
}
public void Export(Assembly assembly, ExportOptions options)
{
var exporterType = assembly.GetType("DllExports.Exporter");
var exportMethod = exporterType.GetMethod("Export");
exportMethod.Invoke(null, new object[]
{
options.Enabled,
options.InputFile,
options.OutputFile,
options.Architectures,
options.ArchitectureNameFormat,
options.RemoveInputFile,
options.KeyFile
});
}
public void Unload()
{
if (unloaded)
return;
loader.Unload();
unloaded = true;
}
}
}
#endif