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

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

问题描述

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

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

  • 类库(.NET 框架)
  • 类库(.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.

那么,Class Library (.NET Standard)Class Library (.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 applications 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 applications 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 applications 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.

类库(.NET Standard)和类库(.NET Core)有什么区别?

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

兼容性:面向 .NET Standard 的库将在任何符合 .NET Standard 的运行时上运行,例如 .NET Core、.NET Framework、单声道/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 Surface Area:.NET Standard 库随NETStandard.Library 中的所有内容一起提供,而.NET Core 库随Microsoft.NETCore.App 中的所有内容一起提供.后者包括大约 20 个附加库,其中一些我们可以手动添加到我们的 .NET Standard 库中(例如 System.Threading.Thread),而其中一些与 .NET Standard 不兼容(例如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.

为什么两者都存在?

暂时忽略库,.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 Standard 库模板可以在多个运行时上运行(以 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). Conversely, 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.

这是一个交互式矩阵,显示了哪些 .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 Standard 类库项目类型之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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