.NET Core和.NET标准类库项目类型之间有什么区别?_.net core和.net区别-程序员宅基地

技术标签: .net-core  .net  class-library  .net-standard  

本文翻译自: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)兼容。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xfxf996/article/details/105464774

智能推荐

class和struct的区别-程序员宅基地

文章浏览阅读101次。4.class可以有⽆参的构造函数,struct不可以,必须是有参的构造函数,⽽且在有参的构造函数必须初始。2.Struct适⽤于作为经常使⽤的⼀些数据组合成的新类型,表示诸如点、矩形等主要⽤来存储数据的轻量。1.Class⽐较适合⼤的和复杂的数据,表现抽象和多级别的对象层次时。2.class允许继承、被继承,struct不允许,只能继承接⼝。3.Struct有性能优势,Class有⾯向对象的扩展优势。3.class可以初始化变量,struct不可以。1.class是引⽤类型,struct是值类型。

android使用json后闪退,应用闪退问题:从json信息的解析开始就会闪退-程序员宅基地

文章浏览阅读586次。想实现的功能是点击顶部按钮之后按关键字进行搜索,已经可以从服务器收到反馈的json信息,但从json信息的解析开始就会闪退,加载listview也不知道行不行public abstract class loadlistview{public ListView plv;public String js;public int listlength;public int listvisit;public..._rton转json为什么会闪退

如何使用wordnet词典,得到英文句子的同义句_get_synonyms wordnet-程序员宅基地

文章浏览阅读219次。如何使用wordnet词典,得到英文句子的同义句_get_synonyms wordnet

系统项目报表导出功能开发_积木报表 多线程-程序员宅基地

文章浏览阅读521次。系统项目报表导出 导出任务队列表 + 定时扫描 + 多线程_积木报表 多线程

ajax 如何从服务器上获取数据?_ajax 获取http数据-程序员宅基地

文章浏览阅读1.1k次,点赞9次,收藏9次。使用AJAX技术的好处之一是它能够提供更好的用户体验,因为它允许在不重新加载整个页面的情况下更新网页的某一部分。另外,AJAX还使得开发人员能够创建更复杂、更动态的Web应用程序,因为它们可以在后台与服务器进行通信,而不需要打断用户的浏览体验。在Web开发中,AJAX(Asynchronous JavaScript and XML)是一种常用的技术,用于在不重新加载整个页面的情况下,从服务器获取数据并更新网页的某一部分。使用AJAX,你可以创建异步请求,从而提供更快的响应和更好的用户体验。_ajax 获取http数据

Linux图形终端与字符终端-程序员宅基地

文章浏览阅读2.8k次。登录退出、修改密码、关机重启_字符终端

随便推点

Python与Arduino绘制超声波雷达扫描_超声波扫描建模 python库-程序员宅基地

文章浏览阅读3.8k次,点赞3次,收藏51次。前段时间看到一位发烧友制作的超声波雷达扫描神器,用到了Arduino和Processing,可惜啊,我不会Processing更看不懂人家的程序,咋办呢?嘿嘿,所以我就换了个思路解决,因为我会一点Python啊,那就动手吧!在做这个案例之前先要搞明白一个问题:怎么将Arduino通过超声波检测到的距离反馈到Python端?这个嘛,我首先想到了串行通信接口。没错!就是串口。只要Arduino将数据发送给COM口,然后Python能从COM口读取到这个数据就可以啦!我先写了一个测试程序试了一下,OK!搞定_超声波扫描建模 python库

凯撒加密方法介绍及实例说明-程序员宅基地

文章浏览阅读4.2k次。端—端加密指信息由发送端自动加密,并且由TCP/IP进行数据包封装,然后作为不可阅读和不可识别的数据穿过互联网,当这些信息到达目的地,将被自动重组、解密,而成为可读的数据。不可逆加密算法的特征是加密过程中不需要使用密钥,输入明文后由系统直接经过加密算法处理成密文,这种加密后的数据是无法被解密的,只有重新输入明文,并再次经过同样不可逆的加密算法处理,得到相同的加密密文并被系统重新识别后,才能真正解密。2.使用时,加密者查找明文字母表中需要加密的消息中的每一个字母所在位置,并且写下密文字母表中对应的字母。_凯撒加密

工控协议--cip--协议解析基本记录_cip协议embedded_service_error-程序员宅基地

文章浏览阅读5.7k次。CIP报文解析常用到的几个字段:普通类型服务类型:[0x00], CIP对象:[0x02 Message Router], ioi segments:[XX]PCCC(带cmd和func)服务类型:[0x00], CIP对象:[0x02 Message Router], cmd:[0x101], fnc:[0x101]..._cip协议embedded_service_error

如何在vs2019及以后版本(如vs2022)上添加 添加ActiveX控件中的MFC类_vs添加mfc库-程序员宅基地

文章浏览阅读2.4k次,点赞9次,收藏13次。有时候我们在MFC项目开发过程中,需要用到一些微软已经提供的功能,如VC++使用EXCEL功能,这时候我们就能直接通过VS2019到如EXCEL.EXE方式,生成对应的OLE头文件,然后直接使用功能,那么,我们上篇文章中介绍了vs2017及以前的版本如何来添加。但由于微软某些方面考虑,这种方式已被放弃。从上图中可以看出,这一功能,在从vs2017版本15.9开始,后续版本已经删除了此功能。那么我们如果仍需要此功能,我们如何在新版本中添加呢。_vs添加mfc库

frame_size (1536) was not respected for a non-last frame_frame_size (1024) was not respected for a non-last-程序员宅基地

文章浏览阅读785次。用ac3编码,执行编码函数时报错入如下:[ac3 @ 0x7fed7800f200] frame_size (1536) was not respected for anon-last frame (avcodec_encode_audio2)用ac3编码时每次送入编码器的音频采样数应该是1536个采样,不然就会报上述错误。这个数字并非刻意固定,而是跟ac3内部的编码算法原理相关。全网找不到,国内音视频之路还有很长的路,音视频人一起加油吧~......_frame_size (1024) was not respected for a non-last frame

Android移动应用开发入门_在安卓移动应用开发中要在活动类文件中声迷你一个复选框变量-程序员宅基地

文章浏览阅读230次,点赞2次,收藏2次。创建Android应用程序一个项目里面可以有很多模块,而每一个模块就对应了一个应用程序。项目结构介绍_在安卓移动应用开发中要在活动类文件中声迷你一个复选框变量

推荐文章

热门文章

相关标签