[英]How to use an DLL load from Embed Resource?
我有一個DLL >> System.Data.SQLite.dll
要以正常方式使用它>只需將其添加為參考和
using System.Data.SQLite;
然后,我可以使用此DLL中的所有函數。
但是 ,我想將我的app.exe和這個DLL合並為一個文件。 我嘗試過使用ILmerge ,但是失敗了。 據我所知, ILmerge無法合並unmanage DLL。
所以,我嘗試了另一種方法>將DLL作為嵌入資源。 我可以使用以下代碼將其作為程序集加載:
Stream stm = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.System.Data.SQLite.dll");
byte[] ba = null;
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = stm.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
ba = ms.ToArray();
}
Assembly sSQLiteDLL = Assembly.Load(ba);
但是,我將如何使用SQLiteDLL中的函數?
我也嘗試在屬性中添加DLL作為資源並加載它像這樣:
public Form1()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
InitializeComponent();
}
System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AppDomain domain = (AppDomain)sender;
if (args.Name.Contains("System_Data_SQLite"))
{
return domain.Load(MyApp.Properties.Resources.System_Data_SQLite);
}
return null;
}
上面解釋了我到目前為止所做的事情,我不知道接下來該怎么做才能使用DLL? 我仍然無法使用DLL中的函數。
例如,當我輸入這個:
SQLiteCommand cmd = new SQLiteCommand();
Visual Studio說:
錯誤21找不到類型或命名空間名稱“SQLiteCommand”(您是否缺少using指令或程序集引用?)
你能分享一下你的見解嗎? 謝謝。
您可以同時嵌入一個程序集並引用它(在VS中)...對於您想要使用它的方式,您需要引用它! 你沒有參考大會的任何理由?
使用嵌入式程序集中的類型(托管)而不引用它有點困難但可能使用反射等 - 請參閱這些鏈接(它們包括參考資料和一些示例代碼等):
在嵌入托管DLL時,您有以下幾種選擇:
要么
要么
AssemblyResolve
處理程序,它在運行時從Resources讀取並將所需的DLL返回給.NET運行時... 這不是使用AppDomain中加載的程序集的方法。 請閱讀本文: 如何:將程序集加載到應用程序域中
簡而言之,您應該使用方法名稱(例如SqlCommand)調用GetMethod(),然后通過.Invoke()方法調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.