【问题标题】:C# - Loading DLL Dynamic - System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested typesC# - 动态加载 DLL - System.Reflection.ReflectionTypeLoadException:无法加载一种或多种请求的类型
【发布时间】:2019-04-24 10:20:31
【问题描述】:

在 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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 哇,很高兴我找到了这个,非常感谢您,先生!
    【解决方案2】:

    Assembly.LoadFile 仅加载 contents of an assembly ,但 Assembly.LoadFrom 完美加载 assembly file (如果有依赖项)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2018-07-02
      • 1970-01-01
      • 2021-06-23
      相关资源
      最近更新 更多