在运行时HOWTO负荷assemby AssemblyResolve事件之前? [英] Howto load assemby at runtime before AssemblyResolve event?

查看:160
本文介绍了在运行时HOWTO负荷assemby AssemblyResolve事件之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我试图执行某种'静态链接组件,我的解决方案中。所以我尝试了以下内容:

Actually i tried to implement some kind of 'statically linked' assemblies, within my solution. So i tried the following:

  • 添加引用到我的组装CopyLocal =假
  • 添加.dll文件本身给我的解决方案,添加为链接
  • 添加.dll文件本身到我的资源,添加资源 - 添加现有文件
  • 添加某种类型的我的组件到Form1的私人MyObject来临时=新的MyObject();
  • Adding a reference to my assembly with CopyLocal = false
  • Adding the .dll file itself to my solution with 'Add as link'
  • Adding the .dll file itself to my resources with 'Add Resource' - 'Add Existing File'
  • Adding some type out of my assembly into Form1 as private MyObject temp = new MyObject();

在这些步骤之后我得到了FileNotFoundException异常预期。因此,让我们尝试加载AssemblyResolveEvent内部的组装与这个快速破解

After these steps i got the FileNotFoundException as expected. So let's try to load the assembly within the AssemblyResolveEvent with this quick hack

AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
    {
        Assembly MyAssembly = AppDomain.CurrentDomain.Load(Properties.Resources.ExternalAssembly);
        return MyAssembly;
    };

所以这个作品!我能到AssemblyResolveEvent内从资源文件中加载我的程序集。但是,这个事件只发生,如果它找不到我的装配其他地方。但是,我怎么能得到我的程序集被加载的之前净试图寻找不同的地方??

So this works! I'm able to load my assembly from a resource file within a AssemblyResolveEvent. But this event only happens, if it couldn't find my assembly anywhere else. But how can i get my assembly be loaded before .Net tries to search the different places??

由于从检查$ P $ 我认为这将pviously引用的程序集的事实可以预先加载程序集到域,这将被采取。

Due to the facts from Checking for Previously Referenced Assemblies i thought it would be possible to load the assembly beforehand into the domain and this would be taken.

我用下面的Main()方法尝试这样的Program.cs在

I tried this within program.cs by using the following Main() method

static void Main()
{
    LoadMyAssemblies();
    AppDomain.CurrentDomain.AssemblyResolve += (sender, e) => LoadMyAssemblies();
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

private static Assembly LoadMyAssemblies()
{
    Assembly result = AppDomain.CurrentDomain.Load(Properties.Resources.MyStaticAssembly);
    return result;
}

但它仍然跑入ResolveEventHandler。而更加美好,如果我再次加载程序集,看一看到 AppDomain.CurrentDomain.GetAssemblies()我可以看到我的程序集被加载两次!!

But it still runs into the ResolveEventHandler. And far more better, if i load the assembly again and take a look into AppDomain.CurrentDomain.GetAssemblies() i can see that my assembly is loaded twice!!

因此​​,任何想法,为什么我加载的程序集将不被考虑,当它在AssemblyResolve事件之前加载?与调试器的帮助下,我还会返回一个空当调用来自AssemblyResolve,但在这种情况下,我得到了一个FileNotFoundException异常作为开头。

So any idea why my loaded assembly won't be taken into account when it is loaded before the AssemblyResolve event?? With help of the debugger i also returned a null when the call came from AssemblyResolve, but in this case i got a FileNotFoundException as at the beginning.

推荐答案

在CLR活页夹不知道LoadMyAssemblies()做的事情是一样的AssemblyResolve事件,他们都试图寻找相同的组装和加载它。

The CLR Binder doesn't know that LoadMyAssemblies() does the same thing as the AssemblyResolve event, and that they are both trying to look for the same assembly and load it.

AssemblyResolve事件总是被炒鱿鱼的点上的活页夹决定,它已经找遍了所有可能的地点(可搜索WRT的应用程序),并不能找到匹配。

AssemblyResolve event always gets fired at the point at which the Binder decides that it has searched all possible locations (that are searchable wrt that application) and could not find a match.

这引出了一个原来的问题,这是,为什么你要静态链接的托管程序?阅读此线程在这个有很多的讨论,<一个href="http://stackoverflow.com/questions/312406/static-linking-advantages">http://stackoverflow.com/questions/312406/static-linking-advantages

This begs the original question, which is, why would you want to statically link your managed assemblies? Read this thread for a lot of discussion on this http://stackoverflow.com/questions/312406/static-linking-advantages

我会继续和回答有关如何避免碰到AssemblyResolve事件的一部分 1)将装配到GAC。至于粘结剂而言,GAC总是获胜。 2)将您的程序集的探测路径,并确保该粘合剂把它捡起(寻找文章运行时如何定位程序集MSDN上获得更多关于这一点)。

I'll go ahead and answer the part on how to avoid hitting the AssemblyResolve event 1) Put the assembly into the GAC. As far as the Binder is concerned, the GAC always wins. 2) Place your assembly in the probing path and make sure that the Binder picks it up (look for the article 'How the runtime locates assemblies' on MSDN for more on this).

这篇关于在运行时HOWTO负荷assemby AssemblyResolve事件之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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