如果 .Net Core 可以在 Windows 上运行,为什么不能在 .Net Framework 中引用 .Net Core DLL? [英] If .Net Core can run on Windows, why can't you reference a .Net Core DLL in .Net Framework?

查看:83
本文介绍了如果 .Net Core 可以在 Windows 上运行,为什么不能在 .Net Framework 中引用 .Net Core DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白为什么 .Net Framework 会导致 .Net Core、IE 出现问题,因为不存在特定于 Windows 平台的 API.但是为什么不能直接引用 .Net Core 作为 .Net Framework 中的库?如果 .Net Core 在 Windows 上运行,是什么阻止了 .Net Framework 应用程序像使用库一样使用 .Net Core?

I get why .Net Framework could cause issues in .Net Core, IE because an API specific to the Windows platform doesn't exist. But why wouldn't you be able to directly reference .Net Core as a library in .Net Framework? If .Net Core runs on windows, what is preventing a .Net Framework app from using .Net Core as if it were a library?

我知道您可以将 .Net Core 库移植到 .Net 标准库,但我的问题是为什么 .Net Framework 不能只引用用 .Net Core 编写的任何内容,因为它无论如何都是跨平台的?

I'm aware that you can port the .Net Core library to a .Net Standard Library, but my question is why .Net Framework can't just reference anything written in .Net Core, since it is cross platform anyway?

推荐答案

目前,.NET Framework 中不仅有 .NET Core 不可用的 API(例如远程处理、WCF 托管、附加应用程序域),而且还有现在也是 .NET Core 中在 .NET Framework 上不可用的 API - 这包括对基类库的有用补充和许多基于 Span<T> 的 API,这些 API 已添加到基类库中.NET Core 2.1 中的 .NET 类型.

Currently, there are not only APIs in .NET Framework that is not available in .NET Core (e.g. Remoting, WCF hosting, additional App Domains), but there are now also APIs in .NET Core that aren't available on .NET Framework - this includes useful additions to the base class library and a lot of Span<T> based APIs that have been added to base .NET types in .NET Core 2.1.

为了创建可在两个框架上使用的库,请使用 .NET Standard.

In order to create libraries that can be used on both frameworks, use .NET Standard.

从技术上讲,.NET Framework 和 .NET Core 之间的最大区别在于(.dll 文件)框架实际承载其实现和类型定义的位置.

Technically, the biggest difference between .NET Framework and .NET Core is where (.dll files) the frameworks actually carry their implementations and type definitions.

虽然 .NET Framework 在 mscorlib.dll 中有很多基本类型,但 .NET Core 可能会在 System.Runtime.dllSystem 中携带它们.Private.CoreLib.dll.

While .NET Framework has a lot of base types in mscorlib.dll, .NET Core may carry them inside System.Runtime.dll or System.Private.CoreLib.dll.

类型引用始终包括程序集名称和命名空间+类型名称.如果您运行的框架在 mscorlib 中定义了 System.Object,但应用程序引用了 [System.Runtime]System.Object,则它可能会失败加载.

A type reference always includes the name of the assembly and the namespace+type name. If the framework you run on has System.Object defined in mscorlib but an application references [System.Runtime]System.Object, it may fail to load.

.NET Core 2.0 已投入精力至少提供类型转发器,以便将引用重定向到正确的程序集.因此,.NET Framework 兼容性可能会在加载 .NET Framework 程序集时将 [mscorlib]System.Object 重定向到 [System.Runtime]System.Object.(请参阅.NET Standard 2.0 使用的兼容性垫片)

.NET Core 2.0 has invested effort to at least provide type forwarders so that references are redirected to the correct assemblies. So the .NET Framework compatibility may redirect [mscorlib]System.Object to [System.Runtime]System.Object when loading .NET Framework assemblies. (see Compatibility shim used by .NET Standard 2.0)

反过来可能也行不通.虽然较新版本的 .NET Framework 提供了许多与 .NET Core 使用的相同的程序集(通过类型转发实现),但它仅保证 .NET Standard 兼容性.如果你的目标是旧版本的 .NET Framework,额外的类型转发 DLL 将被添加到构建输出中以提供这种兼容性(请参阅 为什么我的 .NET Standard NuGet 包会触发这么多依赖项?).

The same may not work the other way around. While newer versions of .NET Framework provide a lot of the same assemblies (implemented via type forwarding) that .NET Core uses, it only guarantees .NET Standard compatibility. If you target older versions of .NET Framework, additional type forwarding DLLs will be added to the build output to provide this compatilbity (see Why does my .NET Standard NuGet package trigger so many dependencies?).

这可能在某种程度上允许在 .NET Framework 上加载一些 .NET Core dll 文件,但不能保证它可以工作.如果 dll 使用 .NET Framework 上不可用的 API,它会失败,但当它引用具有不可用程序集名称的类型时也可能会失败.

This may enable loading some .NET Core dll files on .NET Framework to some degree, but there is no guarantee that it may work. It will fail if the dll uses APIs unavailable on .NET Framework but may also fail when it references a type with an assembly name that is not available.

请注意,这仅适用于加载 dll 文件.项目到项目的引用将失败,因为该工具应禁止从 .NET Framework 项目引用 .NET Core 项目.

Note that this only applies to loading dll files. Project-to-Project references will fail since the tooling should forbid referencing .NET Core projects from .NET Framework projects.

这篇关于如果 .Net Core 可以在 Windows 上运行,为什么不能在 .Net Framework 中引用 .Net Core DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆