[英]How should I force load a referenced assembly?
I have a third-party library that requires an assembly A to be loaded when I call into their code.我有一个第三方库,当我调用他们的代码时需要加载程序集 A。 That assembly is typically installed in the GAC, so I have several options to load it:
该程序集通常安装在 GAC 中,因此我有几个选项可以加载它:
Assembly.Load()
.Assembly.Load()
。 However that requires the full name which I don't feel comfortable to hard code in my program.Assembly.LoadWithPartialName()
.Assembly.LoadWithPartialName()
。 That is an obsolete API of course, and of course I don't feel comfortable to lose control in versioning.Assembly.GetReferencedAssemblies
and force load the matched one.Assembly.GetReferencedAssemblies
并强制加载匹配的,同样的问题。 The C# compiler simply won't reference my assembly even if I put it in the references list. Now what I'm doing is to call typeof(A.Foo).Assembly.GetName()
and ignore the return value.现在我正在做的是调用
typeof(A.Foo).Assembly.GetName()
并忽略返回值。 Is there a better way to do it?有更好的方法吗?
Option 1, for me, would be to reference it in the VS project.对我来说,选项 1 是在 VS 项目中引用它。
But if you want a more passive approach you can use the AppDomain.CurrentDomain.AssemblyResolve event handler.但是,如果您想要更被动的方法,您可以使用 AppDomain.CurrentDomain.AssemblyResolve 事件处理程序。 It executes when an assembly is needed that is not found in the AppDomain.
它在需要在 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()
事件 args 将告诉您正在寻找的程序集,您可以在 go 使用 Assembly.Load() 抓取它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.