C#中:如何嵌入到DLL的resourcefile(程序目录中没有DLL复制) [英] C#: How to embed DLL into resourcefile (no dll copy in program directory)

查看:571
本文介绍了C#中:如何嵌入到DLL的resourcefile(程序目录中没有DLL复制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要X.dll C#应用程序(项目A)。我已经加入产生X.dll到A在Visual Studio中的参考项目。我还增加了X.dll的发行版本在一个资源文件为二进制。我已经告诉项目中的应用于X.dll复制到输出目录。

I have a C# application (project A) that requires X.dll. I have added the project that produces X.dll to A as a reference in visual studio. I have also added the release build of X.dll to a resource file in A as a binary. I have told project A not to copy X.dll to the output directory.

现在我想A.exe时加载,说:嘿,我无法找到这个文件,然后查看资源文件并使用的Assembly.Load(字节[ ])获得X.dll回来。我有一个重新的magicifies代码中的DLL回来,但是这代码永远不会被调用。

Now I want A.exe to load, say "hey I can't find this file", then look in the resource file and use Assembly.Load(byte[]) get X.dll back. I have the code that re-magicifies the DLL back, however this code never get called.

目前我有一个骨简单的项目,只是想获得它的工作。它编译OK。当我运行它,我得到X.dll出现FileNotFoundException

Currently I have a bone simple project, just trying to get it to work. It compile OK. When I run it, I get a FileNotFoundException on X.dll.

我有:

[STAThread]
static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}



但在* CurrentDomain_AssemblyResolve断点*从未被击中。我立即得到一个FileNotFoundException异常。当然,也有一些是我失踪?

But the breakpoint in *CurrentDomain_AssemblyResolve* never gets hit. I get a FileNotFoundException immediately. Surely there is something I a missing?

推荐答案

这个问题似乎很相似,你想达到什么目的,它就职于提问者。

This question seems to be very similar to what you are trying to achieve, and it worked for the asker.

合并.dll文件到C#大会?

您做不同的东西吗?特别是,如果你在目标定位在.NET Framework的旧版本,也许这就是理解为什么你的应用程序行为不同的暗示。

Are you doing something differently? Specifically, if you're targetting an older version of the .NET Framework, maybe that's a hint for understanding why your application is behaving differently.

另一个方向,使用工具如融合日志查看器来分析发生了什么事情,当你试图加载程序集。这可能给一些提示。如果你设法让日志信息,张贴在问题可以帮助别人找到答案

Another direction, use a tool such as Fusion Log Viewer to analyze what was going on when you tried to load the assembly. This may give some more hints. If you manage to get log information, posting it in the question may help someone figure it out.

编辑:另一种解释,下面您的评论

好了,现在我想我知道是什么问题。

Well, now I think I know what the problem is.

在你的方法,你指的是在其它的DLL类型。但你在做它的静态的代码,即您在代码中明确使用类型(而不是它的名字动态加载它)。

In your Main method, you are refering to the type in the other dll. But you are doing it in static code, i.e. you use the type explicitly in the code (as opposed to loading it dynamically by its name).

为什么这是一个问题吗?在CLR试图以加载程序集的为JIT 本身。

Why is this a problem? The CLR tries to load your assembly in order to JIT Main itself.

您而正在编译 FileNotFoundException异常被抛出。 甚至没有开始运行,因此事件处理程序未注册。

Your FileNotFoundException was thrown while Main was being compiled. Main didn't even start running, and therefore your event handler wasn't registered.

有一个简单的方法来检查,如果我是对的是代码更改为类似这样的:

A simple way to check if I'm right is to change the code to something like this:

static public void Main(string[] args)
{
    AppDomain.CurrentDomain.AssemblyResolve += MyEventHandler;
    MyMain();
}

// The CLR will actually try to load your assembly before even starting the execution
// of this method. It needs the assembly in order to JIT the method because it has to 
// know the Thing type.
static public void MyMain()
{
    using(var thing = new Thing())
    {
         // ...
    }
}

重要的区别是,对另一个程序集没有依赖性,所以会有以JIT没有问题,运行它。

The important difference is that Main has no dependency on another assembly, so there will be no problem to JIT and run it.

到了时间 MyMain 正在JIT'ed,事件处理程序已经到位,所以你可以手动加载程序集。

By the time MyMain is being JIT'ed, your event handler is already in place so you can manually load the assembly.

顺便说一句,以避免另一个可能类似的陷阱,使确保在被定义的类没有任何现场与来自其它组件的类型,因为在这种情况下,CLR将尝试之前加载程序集开始,以对其进行编译。

By the way, to avoid another possible similar pitfall, make sure that the class in which Main is defined doesn't have any field with a type from the other assembly, because in this case also, the CLR will try loading the assembly before Main starts, in order to compile it.

这篇关于C#中:如何嵌入到DLL的resourcefile(程序目录中没有DLL复制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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