简体   繁体   English

我应该如何强制加载引用的程序集?

[英]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 中,因此我有几个选项可以加载它:

  1. I can explicitly call Assembly.Load() .我可以显式调用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() .我可以显式调用Assembly.LoadWithPartialName() That is an obsolete API of course, and of course I don't feel comfortable to lose control in versioning.那当然是一个过时的 API,当然,我对在版本控制中失去控制感到不舒服。
  3. I can reference the assembly in my Visual Studio project file, so I always get the version I built against.我可以在我的 Visual Studio 项目文件中引用程序集,所以我总是得到我构建的版本。 However, that won't work unless I create a dummy object in that assembly.但是,除非我在该程序集中创建一个虚拟 object,否则这是行不通的。 The C# compiler simply ignores it if I don't.如果我不这样做,C# 编译器就会忽略它。
  4. Same problem if I call 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. C# 编译器根本不会引用我的程序集,即使我将它放在引用列表中。

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.

相关问题 如何在另一个参考程序集中加载一个参考程序集? - How to load a referenced assembly within another referenced assembly? 动态加载装配并手动强制路径以获取引用的装配 - Dynamically Load Assembly and manually force path to get referenced assemblies 强制C#加载仅在cshtml文件中引用的程序集 - Force C# to load an assembly that is referenced only in a cshtml file 除非我首先在该程序集中实例化一个类,否则无法对引用的程序集进行Assembly.Load(String)。 怎么解决? - Can't do Assembly.Load(String) with a referenced assembly unless I instantiate a class within that assembly first. How to solve? 如何强制加载项目未明确使用的引用程序集 - How to force loading of an referenced assembly that is not explicitly used by the project 程序集加载引用了一些dll - assembly load referenced a few dlls .NET-从另一个程序集引用时如何强制程序集数字证书验证 - .NET - How to force an assembly digital certificate validation when referenced from another assembly 如何强制应用程序从其他程序集中加载.NET类? - How can I force an application to load .NET classes from a different assembly? 我应该如何处理引用实体的持久性? - How should I handle persistence for referenced entities? 如何动态加载项目引用但代码未引用的程序集 - How can I dynamically load assemblies referenced by project, but not referenced in code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM