.NET Core和.NET标准类库项目类型之间有什么区别?

本文翻译自:What is the difference between .NET Core and .NET Standard Class Library project types?

In Visual Studio, there are at least 3 different types of class library you can create: 在Visual Studio中,至少可以创建3种不同类型的类库:

  • Class Library (.NET Framework) 类库(.NET Framework)
  • Class Library (.NET Standard) 类库(.NET标准)
  • Class Library (.NET Core) 类库(.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. 尽管第一个是我们多年来一直在使用的东西,但我一直感到困惑的主要点是何时使用.NET Standard和.NET Core类库类型。 I've been bitten by this recently when attempting to multi-target different framework versions , and creating a unit test project . 最近,当我尝试多目标化不同的框架版本创建一个单元测试项目时,我为此而感到痛苦。

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? 那么, 类库(.NET Standard)类库(.NET Core)有什么区别,为什么两者都存在,何时应在另一种之上使用?


#1楼

参考:https://stackoom.com/question/2uAVq/NET-Core和-NET标准类库项目类型之间有什么区别


#2楼

When should we use one over the other? 我们什么时候应该使用另一个?

The decision is a trade-off between compatibility and API access. 决定是在兼容性和API访问之间进行权衡。

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 Standard库,并且可以减少库可以访问的.NET API表面积。

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 API表面积,请使用.NET Core库,并且可以只允许.NET Core应用程序与库兼容。

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. 例如,面向.NET Standard 1.3的库将与面向.NET Framework 4.6,.NET Core 1.0,Universal Windows Platform 10.0以及支持.NET Standard 1.3的任何其他平台的应用程序兼容 The library will not have access to some parts of the .NET API, though. 但是,该库将无法访问.NET API的某些部分。 For instance, the Microsoft.NETCore.CoreCLR package is compatible with .NET Core but not with .NET Standard. 例如, Microsoft.NETCore.CoreCLR软件包与.NET Core兼容,但与.NET Standard不兼容。

What is the difference between Class Library (.NET Standard) and Class Library (.NET Core)? 类库(.NET Standard)和类库(.NET Core)有什么区别?

The Package-based frameworks section describes the difference. 基于软件包的框架部分描述了差异。

Compatibility: Libraries that target .NET Standard will run on any .NET Standard compliant runtime, such as .NET Core, .NET Framework, Mono/Xamarin. 兼容性:面向.NET Standard的库将在任何.NET Standard兼容的运行时上运行,例如.NET Core,.NET Framework,Mono / Xamarin。 On the other hand, libraries that target .NET Core can only run on the .NET Core runtime. 另一方面,面向.NET Core的库只能在.NET Core运行时上运行。

API Surface Area: .NET Standard libraries come with everything in NETStandard.Library whereas .NET Core libraries come with everything in Microsoft.NETCore.App . API表面积:.NET标准库包含NETStandard.Library所有内容,而.NET Core库包含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 ). 后者包括大约20个其他库,我们可以将其中一些手动添加到.NET Standard库中(例如System.Threading.Thread ),而其中一些库与.NET Standard不兼容(例如Microsoft.NETCore.CoreCLR )。

Also, .NET Core libraries specify a runtime and come with an application model. 此外,.NET Core库指定运行时,并附带一个应用程序模型。 That's important, for instance, to make unit test class libraries runnable. 例如,这对于使单元测试类库可运行非常重要。

Why do both exist? 为什么两者都存在?

Ignoring libraries for a moment, the reason that .NET Standard exists is for portability; 暂时忽略库,.NET Standard存在的原因是为了可移植性。 it defines a set of APIs that .NET platforms agree to implement. 它定义了.NET平台同意实施的一组API。 Any platform that implements a .NET Standard is compatible with libraries that target that .NET Standard. 任何实现.NET Standard的平台都与针对该.NET Standard的库兼容。 One of those compatible platforms is .NET Core. .NET Core是这些兼容平台之一。

Coming back to libraries, the .NET Standard library templates exist to run on multiple runtimes (at the expense of API surface area). 回到库,.NET标准库模板可以在多个运行时上运行(以API表面积为代价)。 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. 相反,.NET Core库模板的存在是为了访问更多API表面积(以兼容性为代价)并指定用于构建可执行文件的平台。


#3楼

.Net Framework and .Net Core are two different implementations of the .Net runtime. .Net Framework.Net Core是.Net运行时的两种不同实现。 Both Core and Framework (but especially Framework) have different profiles that include larger or smaller (or just plain different) selections of the many APIs and assemblies Microsoft has created for .Net, depending on where they are installed and in what profile. 核心和框架(但特别是框架)都有不同的配置文件,这些配置文件包含Microsoft为.Net创建的许多API和程序集的大小选择(或只是略有不同),具体取决于它们的安装位置和配置文件。 For example, there are some different APIs available in Universal Windows apps than in the "normal" Windows profile. 例如,通用Windows应用程序中有一些与“常规” Windows配置文件中不同的API。 Even on Windows, you might have the "Client" profile vs the "Full" profile. 即使在Windows上,您可能也具有“客户端”配置文件和“完整”配置文件。 Additionally, there are other implementations (like Mono) that have their own sets of libraries. 此外,还有其他实现(例如Mono)具有自己的库集。

.Net Standard is a specification for which sets of API libraries and assemblies must be available. .Net Standard是必须具有一组API库和程序集的规范。 An app written for .Net Standard 1.0 should be able to compile and run with any version of Framework, Core, Mono, etc, that advertises support for the .Net Standard 1.0 collection of libraries. 为.Net Standard 1.0编写的应用程序应该能够编译并与任何版本的Framework,Core,Mono等一起运行,以宣传对.Net Standard 1.0库集合的支持。 Similar is true for .Net Standard 1.1, 1.5, 1.6, 2.0, etc. As long as the runtime provides support for the version of Standard targeted by your program, your program should run there. .Net Standard 1.1、1.5、1.6、2.0等也是如此。只要运行时提供对程序所针对的Standard版本的支持,您的程序就应在其中运行。

A project targeted at a version of Standard will not be able to make use of features that are not included in that revision of the standard. 针对Standard版本的项目将无法使用该标准修订版中未包含的功能。 This doesn't mean you can't take dependencies on other assemblies, or APIs published by other vendors (ie: items on NuGet). 这并不意味着您不能依赖于其他程序集或其他供应商发布的API(即:NuGet上的项目)。 But it does mean that any dependencies you take must also include support for your version of .Net Standard. 但这确实意味着您获取的所有依赖项还必须包括对您的.Net Standard版本的支持。 .Net Standard is evolving quickly, but it's still new enough, and cares enough about some of the smaller runtime profiles, that this limitation can feel stifling. .Net Standard正在快速发展,但是它仍然足够新,并且对一些较小的运行时配置文件足够在意,这一限制可能令人窒息。 (Note a year and a half later: this is starting to change, and recent .Net Standard versions are much nicer and more full-featured). (请注意一年半之后:这种情况已经开始改变,并且最新的.Net Standard版本更加美观,功能更加强大)。

On the other hand, an app targeted at Standard should be able to be used in more deployment situations, since in theory it can run with Core, Framework, Mono, etc. For a class library project looking for wide distribution, that's an attractive promise. 另一方面,针对Standard的应用程序应该能够在更多的部署情况下使用,因为从理论上讲,它可以与Core,Framework,Mono等一起运行。对于寻求广泛分发的类库项目,这是一个诱人的希望。 For a class library project used mainly for internal purposes, it may not be as much of a concern. 对于主要用于内部目的的类库项目,它可能不会引起太大的关注。

.Net Standard can also be useful in situations where the SysAdmin team is wanting to move from ASP.Net on Windows to ASP.Net for .Net Core on Linux for philosophical or cost reasons, but the Development team wants to continue working against .Net Framework in Visual Studio on Windows. .Net Standard在出于哲学或成本方面的原因而需要SysAdmin团队从Windows上的ASP.Net迁移到Linux上的.Net Core的ASP.Net的情况下也很有用,但是开发团队希望继续针对.Net进行工作。 Windows上Visual Studio中的框架。


#4楼

So the short answer would be: 因此,简短的答案是:

IAnimal == .NetStandard (General)
ICat == .NetCore (Less General)
IDog == .NetFramework (Specific / oldest and has the most features)

#5楼

A .Net Core Class Library is built upon the .Net Standard . .Net核心类库基于.Net标准构建。 If you want to implement a library that is portable to the .Net Framework , . 如果您想实现可移植到.Net Framework的库,则。 Net Core and Xamarin , choose a .Net Standard Library Net CoreXamarin ,选择.Net标准库

.Net Core will ultimately implement .Net Standard 2 (as will Xamarin and .Net Framework ) .Net Core最终将实施.Net Standard 2Xamarin.Net Framework也会如此)

.Net Core , Xamarin and .Net Framework can, therefore, be identified as flavours of .Net Standard 达网络核心 ,Xamarin.Net Framework可以,因此,被识别为达网络标准口味

To future-proof your applications for code sharing and reuse , you would rather implement .Net Standard libraries. 为了使您的应用程序能够面向未来,以便代码共享和重用,您宁愿实现.Net Standard库。

Microsoft also recommends that you use .NET Standard instead of Portable Class Libraries . Microsoft还建议您使用.NET Standard而不是Portable Class Libraries

To quote MSDN as an authoritative source, .Net Standard is intended to be One Library to Rule Them All . 为了引用MSDN作为权威来源, .Net Standard旨在成为一个统治一切的图书馆 As pictures are worth a thousand words, the following will make things very clear: 由于图片值一千个字,因此以下内容将使您非常清楚:

1. Your current application scenario (fragmented) 1.您当前的应用场景(碎片化)

Like most of us, you are probably in the situation below: (.Net Framework, Xamarin and now .Net Core flavoured applications) 像我们大多数人一样,您可能处于以下情况:(.Net Framework,Xamarin和现在的.Net Core风格的应用程序)

在此处输入图片说明

2. What the .Net Standard Library will enable for you (cross-framework compatibility) 2. .Net标准库将为您提供什么(跨框架兼容性)

Implementing a .Net Standard Library allows code sharing across all these different flavours: 实施.Net标准库可在所有这些不同方面共享代码:

一个图书馆统治一切

For the impatient: 对于急躁的人:

  1. .NET Standard solves the code sharing problem for .NET developers across all platforms by bringing all the APIs that you expect and love across the environments that you need: desktop applications, mobile apps & games, and cloud services: .NET Standard通过在所需的环境(台式机应用程序,移动应用程序和游戏以及云服务)中引入您期望并喜欢的所有API,为所有平台上的.NET开发人员解决了代码共享问题。
  2. .NET Standard is a set of APIs that all .NET platforms have to implement . .NET Standard所有 .NET平台都必须实现的组API This unifies the .NET platforms and prevents future fragmentation . 将统一.NET平台防止将来出现碎片
  3. .NET Standard 2.0 will be implemented by .NET Framework , . .NET Standard 2.0将由.NET Framework实现。 NET Core , and Xamarin . NET CoreXamarin For .NET Core , this will add many of the existing APIs that have been requested. 对于.NET Core ,这将添加许多已请求的现有API。
  4. .NET Standard 2.0 includes a compatibility shim for .NET Framework binaries, significantly increasing the set of libraries that you can reference from your .NET Standard libraries. .NET Standard 2.0包括针对.NET Framework二进制文件的兼容性填充程序 ,大大增加了可从.NET Standard库引用的库集。
  5. .NET Standard will replace Portable Class Libraries (PCLs) as the tooling story for building multi-platform .NET libraries. .NET Standard 将取代可移植类库(PCL),成为构建多平台.NET库的工具。

For a table to help understand what the highest version of .NET Standard that you can target, based on which .NET platforms you intend to run on, head over here . 要获得一张表格,以帮助您了解基于什么版本的.NET平台可以定位的最高版本的.NET Standard,请转至此处

Sources: MSDN: Introducing .Net Standard 来源: MSDN:.Net标准简介


#6楼

.Net Standard exists mainly to improve code sharing and make the APIs available in each .Net implementation more consistent. .Net Standard的存在主要是为了改善代码共享,并使每个.Net实现中可用的API更加一致。

While creating Libraries we can have target as.Net Standard 2.0 so that the library created would be compaitible with different versions of .Net Framework including .Net Core,Mono.. 在创建库时,我们可以将目标指定为.Net Standard 2.0,以便创建的库可与.Net Framework的不同版本(包括.Net Core,Mono)兼容。

发布了0 篇原创文章 · 获赞 137 · 访问量 84万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105464774
net