如何在导入或嵌入资源 DLL 中声明类型

发布于 2025-01-05 03:17:01 字数 1892 浏览 0 评论 0原文

我实际上正在寻找将所有 DLL 和 EXE 合并到单个文件中的解决方案。

我在这里问了一个问题:

<块引用>

如何使用嵌入资源加载 DLL?< /a>

我收到建议,我可以将 DLL 链接为嵌入资源,然后将嵌入 DLL 文件写入内存并使用 DLLImport 导入 DLL。

我按照这里的说明进行操作:

<块引用>

http://weblogs.asp.net/ralfw/archive/2007/02/04/single- assembly-deployment-of-managed-and-unmanagement-code.aspx

下面是我所拥有的已完成:

[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:

How to use an DLL load from Embed Resource?

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:

http://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-code.aspx

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┈┾☆殇 2025-01-12 03:17:01

DllImport 属性用于声明非托管 DLL 中的方法。

由于 System.Data.SQLite.dll 是托管程序集,因此在将程序集保存到磁盘后,您需要做的是通过 Reflection 加载它,如下所示:

using System.Data;
...

var assembly = Assembly.LoadFile(@"path\to\System.Data.SQLite.dll");
var type = assembly.GetType("System.Data.SQLite.SQLiteConnection");
IDbConnection connection = (IDbConnection)Activator.CreateInstance(type);

希望有帮助。

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 via Reflection, something like:

using System.Data;
...

var assembly = Assembly.LoadFile(@"path\to\System.Data.SQLite.dll");
var type = assembly.GetType("System.Data.SQLite.SQLiteConnection");
IDbConnection connection = (IDbConnection)Activator.CreateInstance(type);

Hope that helps.

给我一枪 2025-01-12 03:17:01

如果您想将托管程序集和 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.

圈圈圆圆圈圈 2025-01-12 03:17:01

DllImport 仅适用于本机 DLL。

在嵌入托管 DLL 时,您有多种选择:

或者

  • 使用一些工具,例如 SmartAssembly(商业)
    它可以嵌入和合并其他内容(无需更改源代码)

  • 自己编写的代码不到 10 行(免费但最少的源代码改变)
    将所有需要的依赖项标记为“嵌入资源” - 这样它们就包含在 EXE 文件中...您需要设置一个 AssemblyResolve 处理程序在运行时从资源中读取并将所需的 DLL 返回到 .NET 运行时...

使用以下类型时这样的程序集请参阅这些链接(它们包括参考材料和一些示例代码等):

DllImport is only for native DLLs.

On embedding managed DLLs you have several options:

OR

  • use some tool like SmartAssembly (commercial)
    it can embed and merge among other things (no need to change your source code)

OR

  • code that yourself in less than 10 lines (free but minimal source code change)
    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.):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文