无法在GetTypes方法上加载文件或程序集错误 [英] Could not load file or assembly error on GetTypes method

查看:0
本文介绍了无法在GetTypes方法上加载文件或程序集错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从文件夹中读取dll和进程类型。

xxx.dll
xxx.interfaces.dll

当我加载xxx.dll并对该程序集调用GetTypes时,它引发异常...

{System.IO.FileNotFoundException: Could not load file or assembly 'xxx.Interfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'xxx.Interfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

=== Pre-bind state information ===
LOG: User = xxx
LOG: DisplayName = xxx.Interfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///<myapp>/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: <myapp>inDebugDiagrammer.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: The same bind was seen before, and was failed with hr = 0x80070002.
}

它似乎正在尝试从启动应用程序路径获取依赖的DLL,而不是正在加载的程序集。当我检查加载和处理程序集xxx.Interfaces.dll时,它工作正常。

推荐答案

尝试使用Assembly.Load()Assembly.LoadFrom()LoadFrom()不应抱怨可能由此事实引起的依赖关系

Load-From上下文允许从探测中未包括的路径加载程序集,但允许查找并加载该路径上的依赖项,因为路径信息由上下文维护。

示例代码

var assembly = Assembly.LoadFrom(assemblyPath);
var availableTypes = assembly.GetTypes();

这篇关于无法在GetTypes方法上加载文件或程序集错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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