【发布时间】:2012-02-11 11:07:43
【问题描述】:
我实际上正在寻找将所有 DLL 和 EXE 合并到单个文件中的解决方案。
我在这里问了一个问题:
我收到建议我可以将 DLL 链接为嵌入资源,然后将嵌入的 DLL 文件写入内存并使用 DLLImport 导入 DLL。
我按照这里的说明进行操作:
以下是我所做的:
[DllImport("System.Data.SQLite.dll")]
public static SQLiteConnection sqLiteConnection1 = new SQLiteConnection();
public Form1()
{
ResourceExtractor.ExtractResourceToFile("MyApp.System.Data.SQLite.dll", "System.Data.SQLite.dll");
}
public static class ResourceExtractor
{
public static void ExtractResourceToFile(string resourceName, string filename)
{
if (!System.IO.File.Exists(filename))
using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
byte[] b = new byte[s.Length];
s.Read(b, 0, b.Length);
fs.Write(b, 0, b.Length);
}
}
}
但是 Visual Studio 说这个块会产生错误:
[DllImport("System.Data.SQLite.dll")]
public static SQLiteConnection sqLiteConnection1 = new SQLiteConnection();
错误 1 属性“DllImport”在此声明类型上无效。它仅对“方法”声明有效。
如何在该 DLL 中声明类型?
非常感谢。
【问题讨论】:
-
DllImport 仅适用于非托管代码,此处还需要一个 extern 关键字。
-
由于您的程序在这里只使用托管代码,您关注的博客是针对不同的问题
标签: c# reflection dll embed dllimport