【问题标题】:Reflection, .net project reading dll from .net core project in a different solution反射,.net 项目在不同的解决方案中从 .net 核心项目中读取 dll
【发布时间】:2019-03-14 13:50:54
【问题描述】:

我在 Visual Studio 2017 中有 2 个解决方案。

  • 第一个解决方案有一个项目,该项目从外部解决方案的项目中读取程序集/dll。该项目使用 .net 4.5.2 编码
  • 第二个解决方案有一个项目,该项目带有目标类/dll 以反映到第一个项目。这个项目是用 .net core 2 编写的
  • 这两个项目都有两种不同的解决方案。

第一个项目中的代码:

string pathDllProject_DAL = "E:\\Coding\\Repos\\_order_manager_webapp\\order_manager_webapp_backend\\DAL\\bin\\Debug\\netcoreapp2.2\\DAL.dll";
//This line of code works, I do get the assembly object
Assembly ass_DAL2 = Assembly.LoadFile(pathDllProject_DAL);


Type[] respTypes = ass_DAL2.GetTypes(); //<----Exception Thrown HERE

这是我得到的例外:

'ass_DAL2.GetTypes()' threw an exception of type 'System.Reflection.ReflectionTypeLoadException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146232830
    HelpLink: null
    InnerException: null
    LoaderExceptions: {System.Exception[152]}
    Message: "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."
    Source: "mscorlib"
    StackTrace: "   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)\r\n   at System.Reflection.RuntimeModule.GetTypes()\r\n   at System.Reflection.Assembly.GetTypes()"
    TargetSite: {System.RuntimeType[] GetTypes(System.Reflection.RuntimeModule)}
    Types: {System.RuntimeType[279]}

我想知道从 .net 4.5.2 框架读取在 .net-core 中创建的 DLL 是否不兼容?还是有其他问题?

【问题讨论】:

  • Retrieve the LoaderExceptions property for more information. 你这样做了吗?
  • @thehennyy 是否提示从dll中加载的类型数据不兼容?
  • 那么这就是你的答案。
  • @thehennyy 所以唯一的解决方案是将 Dll 阅读器项目重写为 .net.core 项目?
  • 这取决于您的用例。有一个库可以读取这两种类型的文件,但是它们包含的代码是不可执行的。看看dnlib

标签: c# .net reflection .net-core


【解决方案1】:

我想知道从 .net 4.5.2 框架读取在 .net-core 中创建的 DLL 存在不兼容性?

是的。 .NET Framework 应用程序不能引用 .NET Core 程序集,反之亦然。这就是为什么会有.NET Standard

可以从 .NET Framework 应用程序和 .NET Core 应用程序引用 .NET Standard 项目。

【讨论】:

  • 我能做什么,我可以将第一个项目(读取外部 dll 的项目)创建为 .net 标准项目吗?还是唯一的办法是在.net-core中重新编写项目?
  • @Luther:是否可以将代码转换为 .NET Standard 取决于您使用的 API 和所需的兼容性。请参阅我发布的链接上的矩阵,了解支持每个 .NET Standard 版本的 .NET 版本的概述。另请注意,.NET Standard 不是运行时,而是规范。
  • 是的,可惜我看过了,不兼容,需要重写.net-core中的代码。谢谢
猜你喜欢
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
相关资源
最近更新 更多