WPF 动态加载嵌入主程序的DLL,好处是节省文件数量,坏处是启动影响加载速度。
首先将DLL添加进项目,选择添加现有项,设置生成操作为“嵌入资源”。
代码:
public App() { AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; } private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { if (args.Name.Contains("HandyControl")) { string resourceName = $"TestWebview2.HandyControl.dll"; // 修改为你的命名空间加DLL名 using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { if (stream == null) return null; byte[] assemblyData = new byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } } Debug.Print("--------"+args.Name); return null; } }
标签:主程序,嵌入,stream,assemblyData,DLL,CurrentDomain,WPF,加载 From: https://www.cnblogs.com/wgscd/p/18295601