当前位置: 首页 > news >正文

AssemblyLoadContext 的研究笔记

关于 .NET AssemblyLoadContext 的研究笔记

   public class MyAssemblyLoadContext : AssemblyLoadContext{private readonly AssemblyDependencyResolver _resolver;public MyAssemblyLoadContext(string pluginDllPath) : base(isCollectible: true){// 传入的是“主程序集”路径,resolver 会自动找同名的 .deps.json_resolver = new AssemblyDependencyResolver(pluginDllPath);}// 2. 只要插件里出现“using xxx;”,运行时就会走到这里protected override Assembly? Load(AssemblyName name){string? path = _resolver.ResolveAssemblyToPath(name);if (path != null) return LoadFromAssemblyPath(path);return null; // 返回 null 会让运行时继续默认逻辑}// 3. 非托管 dll (sqlite、libuv …) 同理protected override IntPtr LoadUnmanagedDll(string unmanagedName){string? path = _resolver.ResolveUnmanagedDllToPath(unmanagedName);return path != null ? LoadUnmanagedDllFromPath(path) : IntPtr.Zero;}}
    internal class Program{private static AssemblyLoadContext _alc;static void ALCLoadLib1(){ALCUnload();var file = @"G:\fanbalcode\testwebswagger\ALCHost\Lib1\bin\Debug\net8.0\publish\Lib1.dll";var myALC = new MyAssemblyLoadContext(file);myALC.LoadFromAssemblyPath(file);_alc = myALC;}static void ALCLoadLib2(){ALCUnload();var file = @"G:\fanbalcode\testwebswagger\ALCHost\Lib2\bin\Debug\net8.0\publish\Lib2.dll";var myALC = new MyAssemblyLoadContext(file);myALC.LoadFromAssemblyPath(file);_alc = myALC;}static void ALCUnload(){if (_alc is not null) _alc.Unload();}private static WeakReference GetStudent(){var studentType = _alc.Assemblies.Select(i => i.GetType("Lib.Student")).FirstOrDefault(i => i is not null);var studentIns = Activator.CreateInstance(studentType);return new WeakReference(studentIns);}static void Main(string[] args){ALCLoadLib1();var student1 = GetStudent();ALCLoadLib2();var student2 = GetStudent();Console.WriteLine(student2);// 2. 持续 GC 直到确认释放for (int i = 0; i < 10 && student1.IsAlive; i++){GC.Collect();GC.WaitForPendingFinalizers();Console.WriteLine($"GC #{i + 1}: " + student1.IsAlive);}}}

通过 AssemblyLoadContext 可以实现可卸载的插件系统,对于游戏的热更新或者一些强调扩展性的软件开发有着重要的意义。

http://www.jsqmd.com/news/53752/

相关文章:

  • 2025年度中频炉靠谱厂家排名:500公斤中频炉/铝壳中频炉
  • 2025 年 11 月超声设备厂家权威推荐榜:覆盖河北、山西、辽宁、江苏、浙江、山东、广东等区域,精准成像与高效诊断的行业优选
  • Gerrit新增标签
  • 2025 年 11 月工业气体检测设备厂家权威推荐榜:密闭采样器、气体报警器、气体探测器、在线气体分析仪,精准监测与安全防护首选
  • 2025CMDB 厂商选型攻略:从全栈纳管到一体化运维,企业级配置管理核心指南
  • 2025年质量好的同步反弹缓冲托底轨/缓冲托底轨热门厂家推荐榜单
  • 2025 年超声波清洗设备厂家最新推荐榜,聚焦技术实力与市场口碑深度解析及优质品牌筛选龙门式 / 全自动 / 多臂式 / 履带式 / 通过式超声波清洗设备推荐
  • 2025年知名的工程液压油缸/一顺液压油缸最新TOP品牌厂家排行
  • 2025 年 11 月电动牙刷品牌权威推荐榜:声波/旋转/磁悬浮/叠振/扫振/旋振/智能/便携/儿童/成人全系列深度测评与选购指南
  • 2025年比较好的反弹钢珠轨厂家推荐及选择指南
  • 2025上海留学中介十大排名
  • 详细介绍:macOS 一键免密登录阿里云 ECS:SSH 密钥对认证完整指南
  • 2025 超声波清洗机源头厂家最新推荐排行榜:全品类设备适配多场景,深度解析核心优势全自动 / 多臂式 / 多槽式 / 履带式 / 通过式 / 单槽式 / 摆动式 / 平移式超声波清洗机公司推荐
  • 2025擅长香港留学的中介机构推荐
  • 2025年螺旋电动压力机直销厂家权威推荐:数控电动螺旋压力机/1000吨电动螺旋压力机/直驱电动螺旋压力机源头厂家精选
  • 界面简洁,上手快!适合新手的免费PPT生成软件推荐 - 实践
  • 日活10万的APP,如何精准计算广告收益? - 详解
  • AI如何消除决策疲劳:从厨房到职场的智能工作革命
  • 2025 年 11 月电动伸缩门厂家权威推荐榜:悬浮门/空降闸/工业伸缩门,智能防护与耐用品质深度解析
  • ASCII码进制对照表 - wanghongwei
  • complex复数
  • 大模型推理加速的全景技术指南
  • 行为树BehaviorTree.CPP
  • 2025 最新聚氨酯砂浆厂家推荐榜:基于国际测评数据的欧洲技术与一站式服务甄选指南水性聚氨酯砂浆 / 聚氨酯砂浆自流平公司推荐
  • 2025年热门的污水池清淤机器人厂家推荐及采购指南
  • 2025年口碑好的振动台行业内知名厂家排行榜
  • 2025 年 11 月电线电缆租赁厂家实力推荐榜:电缆线出租,临时用电电缆租赁,电力电缆租赁,空调电缆租赁,高效安全供电解决方案精选
  • 2025 年 11 月牙刷品牌权威推荐榜:涵盖软毛/电动/儿童/便携/声波/护龈/脱敏/旋转式/超软毛牙刷,专业口腔护理与深度清洁口碑之选
  • 2025年11月医药翻译机构综合评测榜:十大服务商详细对比与选择要点
  • 2025氨氮水质监测设备厂家有哪些,废水监测设备厂家有哪些