.net項目dll內嵌加載

1.將dll文件做爲嵌入資源添加到項目;spa

2.程序入口增長如下代碼:code

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);
        }
    }
相關文章
相關標籤/搜索