更新最新框架
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using Fantasy.Helper;
|
||||
|
||||
namespace NB
|
||||
namespace Fantasy
|
||||
{
|
||||
public static class AssemblyHelper
|
||||
{
|
||||
private const string HotfixDll = "Hotfix";
|
||||
private static AssemblyLoadContext? _assemblyLoadContext = null;
|
||||
|
||||
|
||||
public static System.Reflection.Assembly[] Assemblies
|
||||
public static void Initialize()
|
||||
{
|
||||
get
|
||||
{
|
||||
var assemblies = new System.Reflection.Assembly[2];
|
||||
assemblies[0] = LoadEntityAssembly();
|
||||
assemblies[1] = LoadHotfixAssembly();
|
||||
return assemblies;
|
||||
}
|
||||
LoadEntityAssembly();
|
||||
LoadHotfixAssembly();
|
||||
}
|
||||
|
||||
private static System.Reflection.Assembly LoadEntityAssembly()
|
||||
|
||||
private static void LoadEntityAssembly()
|
||||
{
|
||||
return typeof(AssemblyHelper).Assembly;
|
||||
// .NET 运行时采用延迟加载机制,如果代码中不使用程序集的类型,程序集不会被加载
|
||||
// 执行一下,触发运行时强制加载从而自动注册到框架中
|
||||
// 因为AssemblyHelper代码在Entity项目里,所以需要获取这个项目的Assembly
|
||||
// 然后调用EnsureLoaded方法强制加载一下
|
||||
typeof(AssemblyHelper).Assembly.EnsureLoaded();
|
||||
}
|
||||
|
||||
private static System.Reflection.Assembly LoadHotfixAssembly()
|
||||
|
||||
public static System.Reflection.Assembly LoadHotfixAssembly()
|
||||
{
|
||||
if (_assemblyLoadContext != null)
|
||||
{
|
||||
@@ -33,9 +33,16 @@ namespace NB
|
||||
}
|
||||
|
||||
_assemblyLoadContext = new AssemblyLoadContext(HotfixDll, true);
|
||||
var dllBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, $"{HotfixDll}.dll"));
|
||||
var pdbBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, $"{HotfixDll}.pdb"));
|
||||
return _assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
|
||||
var dllBytes = File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, $"{HotfixDll}.dll"));
|
||||
var pdbBytes = File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, $"{HotfixDll}.pdb"));
|
||||
var assembly = _assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
|
||||
// 强制触发 ModuleInitializer 执行
|
||||
// AssemblyLoadContext.LoadFromStream 只加载程序集到内存,不会自动触发 ModuleInitializer
|
||||
// 必须访问程序集中的类型才能触发初始化,这里通过反射调用生成的 AssemblyMarker
|
||||
// 注意:此方法仅用于热重载场景(JIT),Native AOT 不支持动态加载
|
||||
// 拿到Assembly就用EnsureLoaded()方法强制触发
|
||||
assembly.EnsureLoaded();
|
||||
return assembly;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user