.net项目dll内嵌加载

时间:2023-03-09 08:15:11
.net项目dll内嵌加载

1.将dll文件作为嵌入资源添加到项目;

2.程序入口增加以下代码:

public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
} private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var assName = new AssemblyName(args.Name).FullName;
if (args.Name == "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed")
{
var bytes = Music163.Properties.Resources.Newtonsoft_Json;
return Assembly.Load(bytes);//加载资源文件中的dll,代替加载失败的程序集
}
throw new DllNotFoundException(assName);
}
}