我应该如何强制加载引用的程序集? [英] How should I force load a referenced assembly?

查看:147
本文介绍了我应该如何强制加载引用的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要组装第三方库时,我打电话到他们的code加载。该组件一般安装在GAC中,所以我有几个选项来加载它:

I have a third-party library that requires an assembly A to be loaded when I call into their code. That assembly is typically installed in the GAC, so I have several options to load it:

  1. 我可以明确地调用的Assembly.Load()。然而,需要我感觉不舒服硬code。在我的计划全名。
  2. 我可以明确地调用 Assembly.LoadWithPartialName()。这当然是一个过时的API,当然我觉得不舒服失去的版本控制。
  3. 在我引用的程序集在我的Visual Studio项目文件,所以我总是得到我反对建的版本。不过,这不会,除非我创建的组件中的虚拟对象的工作。 C#编译器会忽略它,如果我不知道。
  4. 如果我叫同样的问题 Assembly.GetReferencedAssemblies 和力载荷匹配之一。 C#编译器根本不会引用我的组装,即使我把它放在引用列表中。
  1. I can explicitly call Assembly.Load(). However that requires the full name which I don't feel comfortable to hard code in my program.
  2. I can explicitly call Assembly.LoadWithPartialName(). That is an obsolete API of course, and of course I don't feel comfortable to lose control in versioning.
  3. I can reference the assembly in my Visual Studio project file, so I always get the version I built against. However, that won't work unless I create a dummy object in that assembly. The C# compiler simply ignores it if I don't.
  4. Same problem if I call Assembly.GetReferencedAssemblies and force load the matched one. The C# compiler simply won't reference my assembly even if I put it in the references list.

现在我在做什么是调用的typeof(A.Foo).Assembly.GetName()并忽略返回值。有没有更好的办法做到这一点?

Now what I'm doing is to call typeof(A.Foo).Assembly.GetName() and ignore the return value. Is there a better way to do it?

推荐答案

选项1,对我来说,将是引用它在VS的项目。

Option 1, for me, would be to reference it in the VS project.

但是,如果你愿意,你可以使用AppDomain.CurrentDomain.AssemblyResolve事件处理程序更加被动的做法。在执行时,需要一个组件,其未在AppDomain中找到。事件的args会告诉你,正在寻求大会,你可以去抓住它,在这一点上使用的Assembly.Load()

But if you want a more passive approach you can use the AppDomain.CurrentDomain.AssemblyResolve event handler. It executes when an assembly is needed that is not found in the AppDomain. The event args will tell you the Assembly that is being sought and you can go grab it at that point using Assembly.Load()

这篇关于我应该如何强制加载引用的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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