C# - 加载DLL动态 - System.Reflection.ReflectionTypeLoadException:无法加载一个或多个请求的类型

问题描述 投票:1回答:2

在C#中,当使用下面的代码从文件夹加载DLL时,获取这些下面的堆栈跟踪,当试图获取类型时。

var assembly = Assembly.LoadFile(assemblyInfo.FullName); // assembly loads perfectly using the absolute path.
var types = assembly.GetTypes(); // this line throws the below stacktrace.

堆栈跟踪:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.Assembly.GetTypes()

我也检查了现有的解决方案:Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'Loading DLLs at runtime in C#(没有工作)

c# dll dllimport dll-injection dynamic-dll-import
2个回答
1
投票

解决问题很容易。它只是使用与装配不同的方法。我们应该使用LoadFile而不是使用LoadFrom

所以下面的代码可以有效地解决问题

var assembly = Assembly.LoadFrom(assemblyInfo.FullName); // loads perfectly, absolute path to dll
var types = assembly.GetTypes(); // loads perfectly.

无需使用GetExportedTypes。我们可以得到所有类型。


1
投票

qazxsw poi只加载qazxsw poi,但qazxsw poi完美加载qazxsw poi(和依赖关系如果有)。

© www.soinside.com 2019 - 2024. All rights reserved.