且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果.Net Core可以在Windows上运行,为什么不能在.Net Framework中引用.Net Core DLL?

更新时间:2023-02-16 09:17:34

当前,不仅.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.