![](/img/trans.png)
[英]How can I embed a dll to the output assembly with codedom, and use it as reference using C#?
[英]C# How To Embed And Reference An External Assembly (DLL)
我試圖將一個 DLL 文件嵌入到我的主應用程序 (.exe) 這是我的 program.cs 片段:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
namespace testapp
{
static class Program
{
public static string versioncode = "2";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.AssemblyResolve += (Object sender, ResolveEventArgs args) =>
{
String thisExe = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
System.Reflection.AssemblyName embeddedAssembly = new System.Reflection.AssemblyName(args.Name);
String resourceName = thisExe + "." + embeddedAssembly.Name + ".dll";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return System.Reflection.Assembly.Load(assemblyData);
}
};
Application.Run(new Form1());
}
}
}
我正在嘗試嵌入名為“ThirdDemo.dll”的 DLL,已添加為參考和 Copy Local=false 我還添加為附加文件,而不僅僅是參考和 Build Action=Embedded Resource。
但是我在編譯時遇到了這個錯誤:
stream、null。
我認為resourceName
不正確,因為您從GetManifestResourceStream()
獲得了 null stream 。 如果您嘗試加載不存在的資源,通常會發生這種情況。
您可以使用 ILspy ( https://github.com/icsharpcode/ILSpy/releases ) 之類的東西打開您的 compiled.exe 並查找嵌入式資源的正確名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.