如何使用来自嵌入资源的DLL加载? [英] How to use an DLL load from Embed Resource?

查看:470
本文介绍了如何使用来自嵌入资源的DLL加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DLL >> System.Data.SQLite.dll

I have a DLL >> System.Data.SQLite.dll

要在一个正常的方式来使用它>只是将其添加为参考和

To use it in a normal way > just add it as reference and

using System.Data.SQLite;



那么,我可以使用所有的功能这个DLL里面。

then, I can use all the functions inside this DLL.

但是,我要合并我的 APP.EXE 这DLL到一个单一的文件。
我已经尝试使用 ILmerge ,但失败。据我所知, ILmerge 无法合并取消管理DLL。

But, I want to merge my app.exe and this DLL into one single file. I have tried using ILmerge, but fail. As I know, ILmerge cannot merge unmanage DLL.

所以,我尝试另一种方法>使DLL作为embbed资源。
我能够把它加载与下面的代码的程序集:

So, I tried another method > make the DLL as embbed resource. I am able to load it as an assembly with the below code:

Stream stm = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.System.Data.SQLite.dll");
byte[] ba = null;
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
    int read;
    while ((read = stm.Read(buffer, 0, buffer.Length)) > 0)  
    {
        ms.Write(buffer, 0, read);
    }
    ba = ms.ToArray();
}
Assembly sSQLiteDLL = Assembly.Load(ba);



不过,我要去了如何使用功能SQLiteDLL?

but, how am I going to use the functions in SQLiteDLL?

我也尝试添加DLL作为资源属性和加载它是这样的:

I also tried add the DLL as resource in properties and load it like this:

public Form1()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    InitializeComponent();
}

System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    AppDomain domain = (AppDomain)sender;
    if (args.Name.Contains("System_Data_SQLite"))
    {
        return domain.Load(MyApp.Properties.Resources.System_Data_SQLite);
    }
    return null;
}



以上解释什么,我这么远,我不知道接下来使用DLL怎么办?我仍然无法使用功能的DLL里面

The above explained what I've got so far and I don't know what to do next to use the DLL? I still can't use the functions inside the DLL.

例如,当我键入此:

SQLiteCommand cmd = new SQLiteCommand();

<:



Visual Studio的说EM>错误21类型或命名空间名称'SQLiteCommand'找不到(是否缺少using指令或程序集引用?)的

你能分享您的见解?谢谢你。

Can you share your insight? Thanks.

推荐答案

您可以嵌入一个程序集,同时引用它(在VS)...为你的方式要使用它,你需要参考吧!你不引用大会的任何原因。

You can embed an assembly AND reference it (in VS) at the same time... for the way you want to use it you need to reference it! Any reason you don't reference the Assembly ?

从嵌入式大会使用类型(管理),而不引用实在是有点困难,但可以使用反射等等 - 见这些链接(它们包括参考材料和一些示例代码等):

Using a Type from an embedded Assembly (managed) without referencing it is a bit harder but possible using Reflection etc. - see these links (they include reference material AND some sample code etc.):

  • http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
  • http://stackoverflow.com/a/57450/847363
  • http://msdn.microsoft.com/en-us/library/h538bck7.aspx (loads an ssembly from a byte array so there is no need to write that assembly to the filesystem)
  • http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettype.aspx
  • http://www.codeproject.com/Articles/32828/Using-Reflection-to-load-unreferenced-assemblies-a

在嵌入托管的DLL,你有几种选择:

On embedding managed DLLs you have several options:

  • use ILMerge (free)
    For howto see here and here


  • 使用类似的 SmartAssembly (商业)

    它可以嵌入和除其他外合并(无需改变你的源代码)

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


  • 代码,自己少超过10行(免费的,但很少改变源代码)

    标志所有需要的依赖关系为嵌入的资源 - 这种方式,他们都包含在EXE文件......你需要设置一个 AssemblyResolve 处理它在运行时从资源读取并返回所需的DLL到.NET运行时...

  • 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...

这篇关于如何使用来自嵌入资源的DLL加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆