如果.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?

查看:207
本文介绍了如果.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.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.dll System中.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添加到生成输出中以提供这种兼容性(请参阅

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天全站免登陆