2013-12-16 96 views
1

我在读取文件夹中的dll和进程类型。GetTypes方法无法加载文件或程序集错误

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时,它工作正常。

+0

您是否尝试过所有的'Assembly.Load'和'Assembly.LoadFrom'? –

+0

我正在使用LoadFile api。感谢您的参考,LoadFrom正在工作。 –

+0

你应该给它作为答案。对于处于我的情况的其他人可能会有所帮助。我会尽力在我的观察之上。 –

回答

4

尝试使用Assembly.Load()Assembly.LoadFrom()LoadFrom()不应抱怨这可能是由这个事实

负载从上下文允许从一个路径加载的组件中探测不包括在内,并且还允许所引起的依赖该路径上的依赖性被发现和加载因为路径信息由上下文维护。

示例代码

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

为什么'LoadFrom'不会抱怨依赖关系?它是如何工作的? –

+0

根据MSDN上的阅读编辑我的答案。希望能取悦你。 –

+0

你救了我的工作日,谢谢!我使用Assembly.LoadFile()加载了带有OP所描述异常的程序集。 –

相关问题