无法在GetTypes方法上加载文件或程序集错误

时间:2013-12-16 07:14:09

标签: c# .net dll reflection .net-assembly

我正在从文件夹中读取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>\bin\Debug\Diagrammer.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.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时,它工作正常。

1 个答案:

答案 0 :(得分:5)

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

  

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

示例代码

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