.NET Core和.NET标准类库项目类型之间有什么区别? [英] What is the difference between .NET Core and .NET Standard Class Library project types?

查看:106
本文介绍了.NET Core和.NET标准类库项目类型之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio中,可以创建至少3种不同类型的类库:

In Visual Studio, there are at least 3 different types of class library you can create:


  • 类库(.NET Framework)

  • 类库(.NET标准)

  • 类库(.NET Core)

虽然第一个是我们多年来一直在使用的东西,但是我一直感到困惑的一个主要问题是何时使用.NET Standard和.NET Core类库类型。我最近尝试多目标不同框架版本创建单元测试项目

While the first is what we've been using for years, a major point of confusion I've been having is when to use the .NET Standard and .NET Core class library types. I've been bitten by this recently when attempting to multi-target different framework versions, and creating a unit test project.

那么,类库(.NET标准)类库(.NET Core)之间有什么区别,为什么两者都存在,以及何时应该

So, what is the difference between Class Library (.NET Standard) and Class Library (.NET Core), why do both exist, and when should we use one over the other?

推荐答案


何时应该在另一个上使用?

When should we use one over the other?

决定要在兼容性和API访问之间进行权衡。

The decision is a trade-off between compatibility and API access.

如果要增加将与库兼容的应用程序的数量,并且可以减少.NET API的使用,请使用.NET Standard库。库可以访问的表面积。

Use a .NET Standard library when you want to increase the number of apps that will be compatible with your library, and you are okay with a decrease in the .NET API surface area your library can access.

当您想增加库可以访问的.NET API表面积时,请使用.NET Core库,并且可以允许仅与您的库兼容的.NET Core应用。

Use a .NET Core library when you want to increase the .NET API surface area your library can access, and you are okay with allowing only .NET Core apps to be compatible with your library.

例如,面向.NET Standard 1.3的库将与目标为.NET Framework 4.6,.NET Core 1.0,通用Windows的应用程序兼容平台10.0,以及任何其他支持.NET Standard 1.3的平台。但是,该库将无法访问.NET API的某些部分。例如, Microsoft.NETCore.CoreCLR 软件包与.NET Core兼容,但与.NET Standard不兼容。

For example, a library that targets .NET Standard 1.3 will be compatible with apps that target .NET Framework 4.6, .NET Core 1.0, Universal Windows Platform 10.0, and any other platform that supports .NET Standard 1.3. The library will not have access to some parts of the .NET API, though. For instance, the Microsoft.NETCore.CoreCLR package is compatible with .NET Core but not with .NET Standard.


Class之间有什么区别库(.NET标准)和类库(.NET Core)?

What is the difference between Class Library (.NET Standard) and Class Library (.NET Core)?

基于软件包的框架部分描述了区别。

兼容性:面向.NET Standard的库在任何.NET Standard兼容运行时上运行,例如.NET Core,.NET Framework,Mono / Xamarin。另一方面,面向.NET Core的库只能在.NET Core运行时上运行。

Compatibility: Libraries that target .NET Standard will run on any .NET Standard compliant runtime, such as .NET Core, .NET Framework, Mono/Xamarin. On the other hand, libraries that target .NET Core can only run on the .NET Core runtime.

API领域:.NET标准库随附 NETStandard.Library 中的所有内容,而.NET Core库随附 Microsoft.NETCore.App 中的所有内容。后者包括大约20个其他库,我们可以将其中一些手动添加到我们的.NET Standard库中(例如 System.Threading.Thread ),其中一些不兼容.NET标准(例如 Microsoft.NETCore.CoreCLR )。

API Surface Area: .NET Standard libraries come with everything in NETStandard.Library whereas .NET Core libraries come with everything in Microsoft.NETCore.App. The latter includes approximately 20 additional libraries, some of which we can add manually to our .NET Standard library (such as System.Threading.Thread) and some of which are not compatible with the .NET Standard (such as Microsoft.NETCore.CoreCLR).

.NET Core库还指定运行时,并附带一个应用程序模型。例如,这对于使单元测试类库可运行非常重要。

Also, .NET Core libraries specify a runtime and come with an application model. That's important, for instance, to make unit test class libraries runnable.


为什么两者都存在?

Why do both exist?

暂时忽略库,.NET Standard存在的原因是为了可移植性;它定义了.NET平台同意实施的一组API。任何实现.NET Standard的平台都与针对该.NET Standard的库兼容。 NET Core是兼容的平台之一。

Ignoring libraries for a moment, the reason that .NET Standard exists is for portability; it defines a set of APIs that .NET platforms agree to implement. Any platform that implements a .NET Standard is compatible with libraries that target that .NET Standard. One of those compatible platforms is .NET Core.

回到库中,.NET标准库模板可以在多个运行时中运行(以API表面积为代价) )。相反,.NET Core库模板的存在是为了访问更多API表面积(以兼容性为代价),并指定用于构建可执行文件的平台。

Coming back to libraries, the .NET Standard library templates exist to run on multiple runtimes (at the expense of API surface area). Obversely, the .NET Core library templates exist to access more API surface area (at the expense of compatibility) and to specify a platform against which to build an executable.

< href = https://dotnet.microsoft.com/platform/dotnet-standard#versions rel = noreferrer>这是一个交互式矩阵,它显示哪个.NET Standard支持哪个.NET实现(一个或多个)。 )以及可用的API表面积有多少。

Here is an interactive matrix that shows which .NET Standard supports which .NET implementation(s) and how much API surface area is available.

这篇关于.NET Core和.NET标准类库项目类型之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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