alo*_*ont 11 c# embed sqlite resources
我有一个DLL >> System.Data.SQLite.dll
要以正常方式使用它>只需将其添加为参考和
using System.Data.SQLite;
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用此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);
Run Code Online (Sandbox Code Playgroud)
但是,我将如何使用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;
}
Run Code Online (Sandbox Code Playgroud)
上面解释了我到目前为止所做的事情,我不知道接下来该怎么做才能使用DLL?我仍然无法使用DLL中的函数.
例如,当我输入这个:
SQLiteCommand cmd = new SQLiteCommand();
Run Code Online (Sandbox Code Playgroud)
Visual Studio说:
错误21找不到类型或命名空间名称"SQLiteCommand"(您是否缺少using指令或程序集引用?)
你能分享一下你的见解吗?谢谢.
您可以同时嵌入一个程序集并引用它(在VS中)...对于您想要使用它的方式,您需要引用它!你没有参考大会的任何理由?
使用嵌入式程序集中的类型(托管)而不引用它有点困难但可能使用反射等 - 请参阅这些链接(它们包括参考资料和一些示例代码等):
在嵌入托管DLL时,您有以下几种选择:
要么
要么
AssemblyResolve处理程序,它在运行时从资源中读取并将所需的DLL返回给.NET运行时...| 归档时间: |
|
| 查看次数: |
17468 次 |
| 最近记录: |