gpt4 book ai didi

C# - 动态加载 DLL - System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types

转载 作者:行者123 更新时间:2023-12-04 00:26:54 27 4
gpt4 key购买 nike

在 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# (没用)

最佳答案

问题的解决方案非常简单。它只是使用与程序集不同的方法。我们应该使用 LoadFrom

而不是使用 LoadFile

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

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

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

LoadFrom 与其他 DLL 进行自动反射绑定(bind),但是 loadfile 不这样做。

这解决了该导出问题。

关于C# - 动态加载 DLL - System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55827738/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com