简体   繁体   English

如何在Imported或Embed Resource DLL中声明类型

[英]How to declare type in an Imported or Embed Resource DLL

I'm actually looking for solution merge all DLL and EXE into single file. 我实际上正在寻找将所有DLL和EXE合并到单个文件中的解决方案。

I have asked a question at here: 我在这里问了一个问题:

How to use an DLL load from Embed Resource? 如何使用Embed Resource的DLL加载?

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. 我收到了可以将DLL作为嵌入资源进行链接的建议,然后将嵌入的DLL文件写入内存并使用DLLImport导入DLL。

I followed the instructions here: 我按照这里的说明操作:

http://weblogs.asp.net/ralfw/archive/2007/02/04/single-assembly-deployment-of-managed-and-unmanaged-code.aspx 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: 但是Visual Studio说这个块会产生错误:

[DllImport("System.Data.SQLite.dll")]
public static SQLiteConnection sqLiteConnection1 = new SQLiteConnection();

Error 1 Attribute 'DllImport' is not valid on this declaration type. 错误1属性'DllImport'在此声明类型上无效。 It is only valid on 'method' declarations. 它仅对'方法'声明有效。

How to declare the type inside that DLL? 如何在DLL中声明类型?

Thanks you very much. 非常感谢你。

The DllImport attribute is used to declare methods from unmanaged DLLs. DllImport属性用于从非托管DLL声明方法。

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: 由于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);

Hope that helps. 希望有所帮助。

If you want to stuff managed assemblies and the exe in one file, I suggest you look at ILMerge . 如果你想在一个文件中填充托管程序集和exe,我建议你看一下ILMerge

It is much easier to use than manually doing things with resources. 它比使用资源手动操作更容易使用。

DllImport is only for native DLLs. DllImport仅适用于本机DLL。

On embedding managed DLLs you have several options: 嵌入托管DLL时,您有几种选择:

OR 要么

  • use some tool like SmartAssembly (commercial) 使用SmartAssembly之类的工具(商业)
    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) 自己少于10行的代码(免费但最少的源代码更改)
    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... 将所有需要的依赖项标记为“嵌入式资源”-这样,它们就将包含在EXE文件中...您需要设置一个AssemblyResolve处理程序,该处理程序在运行时从资源中读取并将所需的DLL返回到.NET运行时...

On using a type from such an Assembly see these links (they including reference material AND some sample code etc.): 在使用此类Assembly中的类型时,请参见以下链接(它们包括参考资料和一些示例代码等):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM