c# - Assembly.LoadFile 在可执行文件的位置查看依赖关系

标签 c# .net reflection

我写了一个包装器类,它使用反射加载其他程序集。 Wrapper.dll 也被进程通过反射加载,下面是解释场景的图表。

Process.exe  
         |___Loads__ Wrapper.dll  
                         |_______Loads___1.dll  
                                           |___ Depends___ xyz.dll  

文件结构如下

Root
  |___A  
  |   |__Process.exe  
  |___B  
      |__Wrapper.dll   
      |__C
         |__1.dll
         |__xyz.dll

我正在使用 Assembly.LoadFile(@"c:\root\B\C\1.dll"); 函数加载 1.dll Wrapper.dll 但是它找不到依赖项,因为它试图在 A 目录中找到依赖项,该目录与我们拥有进程可执行文件的目录相同。

最佳答案

我找到了解决方案,我只是将 Assembly.LoadFile 更改为 Assembly.LoadFrom 并且它的加载非常好。您可以阅读说明 here但以下是它的外壳。

Load-From Context

The load-from context lets you load an assembly from a path that is not under the application path, and therefore is not included in probing. It enables dependencies to be located and loaded from that path, because the path information is maintained by the context. In addition, assemblies in this context can use dependencies that are loaded into the default load context.

关于c# - Assembly.LoadFile 在可执行文件的位置查看依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36075829/

相关文章:

c# - 使用 ObjectDataSource 和 DataObjectTypeName,如何处理只有一个 Id 参数的删除方法?

java - 在 Java 中,是否有更好的方法来替代 getter 和 setter 方法?

c# - 在 MVC 4 中为 3 层架构配置 Ninject

c# 2008 SQL Server 快速连接字符串

c# - 关闭客户端时出现 SignalR Websocket 异常

javascript - 我如何从 asp.net c# 后面的代码中的 div 中获取文本?

.net - 构建任务之前是否有 .NET Core CLI pre?

c# - 使用 csvHelper 处理 csv 文件中的无效条目

ruby-on-rails - 我可以访问在 ruby​​ on Rails 中调用实例方法的集合吗

c# - 在运行时更改 C# 中字段的类型