我有一个 DLL >> System.Data.SQLite.dll
要以正常的方式使用它 > 只需将其添加为引用即可
using System.Data.SQLite;
接下来,我可以使用此DLL中的所有函数。
但是,我想将我的app.exe和这个DLL合并成一个单独的文件。 我尝试过使用ILmerge,但失败了。据我所知,ILmerge无法合并非托管的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 指令或程序集引用?)
你能分享一下你的想法吗?谢谢。