[英]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. 希望有所帮助。
DllImport
is only for native DLLs. DllImport
仅适用于本机DLL。
On embedding managed DLLs you have several options: 嵌入托管DLL时,您有几种选择:
OR 要么
OR 要么
AssemblyResolve
handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime... 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.