在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#(没有工作)
解决问题很容易。它只是使用与装配不同的方法。我们应该使用LoadFile
而不是使用LoadFrom
所以下面的代码可以有效地解决问题
var assembly = Assembly.LoadFrom(assemblyInfo.FullName); // loads perfectly, absolute path to dll
var types = assembly.GetTypes(); // loads perfectly.
无需使用GetExportedTypes。我们可以得到所有类型。
qazxsw poi只加载qazxsw poi,但qazxsw poi完美加载qazxsw poi(和依赖关系如果有)。