如何在导入或嵌入资源 DLL 中声明类型
我实际上正在寻找将所有 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 内的类型?
非常感谢你。
I'm actually looking for solution merge all DLL and EXE into single file.
I have asked a question at here:
and I received suggestion that I can link the DLL as embed resource, then write the embed DLL file into memory and use DLLImport to import the DLL.
I followed the instructions here:
and below is what I have done:
[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);
}
}
}
but Visual Studio says that this block creates an error:
[DllImport("System.Data.SQLite.dll")]
public static SQLiteConnection sqLiteConnection1 = new SQLiteConnection();
Error 1 Attribute 'DllImport' is not valid on this declaration type. It is only valid on 'method' declarations.
How to declare the type inside that DLL?
Thanks you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DllImport
属性用于声明非托管 DLL 中的方法。由于 System.Data.SQLite.dll 是托管程序集,因此在将程序集保存到磁盘后,您需要做的是通过 Reflection 加载它,如下所示:
希望有帮助。
The
DllImport
attribute is used to declare methods from unmanaged DLLs.Since
System.Data.SQLite.dll
is a managed assembly, what you need to do, after saving your assembly to disk, is to load it viaReflection
, something like:Hope that helps.
如果您想将托管程序集和 exe 填充到一个文件中,我建议您查看 ILMerge。
它比手动使用资源更容易使用。
If you want to stuff managed assemblies and the exe in one file, I suggest you look at ILMerge.
It is much easier to use than manually doing things with resources.
DllImport
仅适用于本机 DLL。在嵌入托管 DLL 时,您有多种选择:
有关如何查看此处和此处
或者
它可以嵌入和合并其他内容(无需更改源代码)
或
将所有需要的依赖项标记为“嵌入资源” - 这样它们就包含在 EXE 文件中...您需要设置一个
AssemblyResolve
处理程序在运行时从资源中读取并将所需的 DLL 返回到 .NET 运行时...使用以下类型时这样的程序集请参阅这些链接(它们包括参考材料和一些示例代码等):
DllImport
is only for native DLLs.On embedding managed DLLs you have several options:
For howto see here and here
OR
it can embed and merge among other things (no need to change your source code)
OR
mark all needed dependencies as "embedded resource" - this way they are included in the EXE file... you need to setup an
AssemblyResolve
handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime...On using a type from such an Assembly see these links (they including reference material AND some sample code etc.):