简体   繁体   English

.Net Core,Portable,Standard,Compact,UWP和PCL之间的区别?

[英]Difference between .Net Core, Portable, Standard, Compact, UWP, and PCL?

I've heard of 我听说过

  • .Net Core .Net核心
  • .Net Portable .Net Portable
  • .Net Standard .Net标准
  • .Net Compact .Net Compact
  • Universal Windows Platform 通用Windows平台
  • Portable Class Libraries 便携式类库

All of these were explained to me as "a subset of the full .Net that allows you to target multiple platforms" . 所有这些都被解释为“完整.Net的一个子集,允许您定位多个平台” So my questions are 所以我的问题是

  1. What's the difference!? 有什么不同!?
  2. If I want to write a library that's usable to as large an audience as possible, which one (or more than one) of these do I need to use? 如果我想编写一个可供尽可能多的受众使用的库, 我需要使用哪一个(或多个)

(My specific situation: I have a library that targets .Net 2.0, .Net 4.5, and UWP. Targeting UWP required creating a new VS project and linking all the existing files, which is a huge pain. Now someone is telling me it doesn't work for PCL, and from the sound of it I have to do it AGAIN for .Net Standard!?) (我的具体情况:我有一个针对.Net 2.0,.Net 4.5和UWP的库。针对UWP需要创建一个新的VS项目并链接所有现有文件,这是一个巨大的痛苦。现在有人告诉我它没有不适用于PCL,从它的声音我必须再次为.Net标准!?)

I'll answer your second question first: 我先回答你的第二个问题:

I have a library that targets .Net 2.0, .Net 4.5, and UWP. 我有一个针对.Net 2.0,.Net 4.5和UWP的库。 Targeting UWP required creating a new VS project and linking all the existing files, which is a huge pain. 针对UWP需要创建一个新的VS项目并链接所有现有文件,这是一个巨大的痛苦。 Now someone is telling me it doesn't work for PCL, and from the sound of it I have to do it AGAIN for .Net Standard!?) 现在有人告诉我它对PCL不起作用,从它的声音我必须再次为.Net标准做!?)

If I want to write a library that's usable to as large an audience as possible, which one (or more than one) of these do I need to use? 如果我想编写一个可供尽可能多的受众使用的库,我需要使用哪一个(或多个)?

Short answer: you should target netstandard . 简短的回答:你应该以netstandard目标。 Use the lowest version that has all the APIs you need. 使用包含所需API的最低版本 You can use a tool like API Port to check your existing project for compatibility with a given netstandard version . 您可以使用API端口等工具检查现有项目是否与给定的netstandard版本兼容

Unfortunately, this approach will leave behind older platforms, in your case, .NET 2.0. 不幸的是,这种方法将留下旧的平台,在您的情况下,.NET 2.0。 If maintaining .NET 2.0 support is necessary, then you'll need a separate project (with linked files) to build a separate .NET 2.0 assembly. 如果需要维护.NET 2.0支持,那么您将需要一个单独的项目(带有链接文件)来构建单独的.NET 2.0程序集。

On to the details... 关于细节......

What's the difference!? 有什么不同!?

  • .Net Standard ( netstandard ) - this is the new cross-platform BCL API. .Net Standardnetstandard ) - 这是新的跨平台BCL API。 It's a "standard" in the sense that it's just an API definition and not an implementation. 它是一个“标准”,因为它只是一个API定义,而不是一个实现。 The idea is that you can compile your library to (a version of) this API and it will run on any platform that supports that version. 我们的想法是您可以将库编译为此API的(版本),它将在支持该版本的任何平台上运行。
  • .Net Core - you can think of this as a reference implementation of netstandard (with a few extra bits). .Net Core - 您可以将其视为netstandard的参考实现(带有一些额外的位)。 It is a cross-platform implementation of that API. 它是该API的跨平台实现 It is possible that UIs and other frameworks may build on it, but for now its only sure foothold is acting as the platform of choice for ASP.NET Core. UI和其他框架可能构建在它上面,但是现在它唯一可靠的立足点是作为ASP.NET Core的首选平台。 [Side note: for historical reasons, ".NET Core" is completely different than the netcore NuGet target; [旁注:由于历史原因,“.NET Core”与netcore NuGet目标完全不同 ; when you're in a NuGet context, netcore means "Windows 8/8.1/10"] . 当你在NuGet上下文时, netcore意味着“Windows 8 / 8.1 / 10”]
  • .Net Portable and Portable Class Libraries - Portable Class Libraries (PCLs) are a least-common-denominator approach to providing a cross-platform API. .Net便携式可移植类库 - 可移植类库(PCL)是提供跨平台API的最常见分母方法。 They cover a wide range of target platforms , but they are incomplete and not future-proof. 它们涵盖了广泛的目标平台 ,但它们不完整且不具备前瞻性。 They have essentially been replaced by netstandard . 它们基本上已被netstandard取代。
  • .Net Compact - This is a completely different .NET framework with its own unique API. .Net Compact - 这是一个完全不同的.NET框架,它有自己独特的API。 It is completely incompatible with any other framework, PCL, or netstandard version; 它与任何其他框架,PCL或netstandard版本完全不兼容; as such, it is much more difficult to support than any other platform. 因此,支持比任何其他平台都困难得多。 However, it is still used on devices with tight memory constraints. 但是,它仍然在具有严格内存限制的设备上使用。
  • Universal Windows Platform - This was a Win10-era merging of the API between Windows Phone and desktop, allowing Windows Store apps/libraries to be written for both platforms. 通用Windows平台 - 这是Windows Phone和桌面之间的Win10时代的API合并,允许为两个平台编写Windows应用商店应用/库。 This has essentially been replaced by netstandard . 这基本上已被netstandard取代。

As linked in the comments, there is already a description by Microsoft that outlines all of this information. 正如评论中所链接的那样,Microsoft已经描述了所有这些信息。 However, by your response, it seems as though you didn't fully understand the entire thing. 但是,根据你的回答,似乎你并没有完全理解整个事情。 It's very long, so here's (hopefully) a tl;dr version. 长,所以这里(希望)是一个tl; dr版本。

We'll start with the following table from the above link, and hopefully this will clear up some of the confusion: 我们将从上面链接的下表开始,希望这将消除一些混乱:

.NET标准合规性

A brief overview for other pople who find this later: The .NET libraries have gone over a lot of changes and ports over its 15 years of existence. 稍后对其他人发现的概述:.NET库在其存在的15年中经历了很多变化和端口。 In that time, many smartphones are now nearly as powerful as some desktops that were in use back in 2001. In that intermin, subsets of the .NET framework were created (and quickly abandoned) for use in different platforms. 在那段时间里,许多智能手机现在几乎与2001年使用的一些桌面一样强大。在这个国际米兰中, .NET框架的子集被创建(并且很快被放弃)用于不同的平台。 With Satya Nadella's new approach to making .NET as wide-reaching of a platform as possible, things needed to change. 随着Satya Nadella将.NET作为尽可能广泛的平台的新方法,需要改变一些事情。

Being a 15 year old technology, things needed to be improved. 作为一项已有15年历史的技术,需要改进。 .NET Core has been worked on since 2014 as a complete overhaul of the .NET architecture. 自2014年以来, .NET Core一直是.NET架构的彻底改革之一。 It has been rewritten from the ground up as a new version of the .NET language. 它已经从头开始重写为.NET语言的新版本。 One of the goals of Core was to enable cross-platform deployment. Core的目标之一是实现跨平台部署。 Whether it's an app to run on an iPhone/Android/XBox One, or a website that can be hosted in IIS, or on a Linux box, .NET Core has you covered. 无论是在iPhone / Android / XBox One上运行的应用程序,还是可以在IIS或Linux机器上托管的网站,.NET Core都能满足您的需求。 It does this by several different ways, including not requiring the .NET Framework to be installed on the machine, and will instead package the necessary libraries with your solution. 它通过几种不同的方式实现这一点,包括不需要在机器上安装.NET Framework,而是使用您的解决方案打包必要的库。

Most notably with .NET Core is the drastic changes to ASP.NET. 最值得注意的是,.NET Core是对ASP.NET的重大改变。 The old System.Web is completely gone and rewritten to be as performant as possible with impressive results . 旧的System.Web完全消失并重写为尽可能高效,效果令人印象深刻 The separate WebApi controllers are gone, as everything is done within a single controller. 单独的WebApi控制器已经消失,因为一切都在一个控制器内完成。 The entire process is now opt-in as opposed to defaulting to allow things you might not want to. 整个过程现在是选择加入,而不是默认允许您可能不想要的事情。

However, we as developers will want to migrate some applications eventually, so how can we be sure that the code we've already written hasn't had several little minor name changes to methods thus breaking the compilation of giant solutions? 但是,我们作为开发人员最终会想要迁移一些应用程序,那么我们如何确保我们已经编写的代码没有对方法进行一些小的名称更改,从而打破了巨型解决方案的编译? In comes the .NET Standard . 来自.NET标准 This is a set of APIs that must be implemented in order for your platform to call itself ".NET". 这是一组必须实现的API,以便您的平台将自己称为“.NET”。

As the basic .NET Framework we've been working with for years is well-established, it was used as the basis for what the Standard will encompass. 作为我们多年来一直使用的基本.NET框架已经很成熟,它被用作标准将包含的基础。 However, everything isn't included, as what would be the point? 但是,一切都不包括在内,重点是什么? So the Standard is only what common APIs will exist between the various types of the .NET platforms. 因此,标准只是各种类型的.NET平台之间存在的通用API。 You will not eventually be writing code in ".NET Standard". 您最终不会在“.NET Standard”中编写代码。

Xamarin (included in the above table) was purchased by Microsoft in 2016, and that technology was used to help build (or at least inspire) .NET Core to be cross-platform. Xamarin(包含在上表中)于2016年被Microsoft收购 ,该技术用于帮助构建(或至少激发).NET Core跨平台。 It still exists as a tool, but in the same veins as it was used in the past. 它仍然作为一种工具存在,但与过去使用的静脉相同。 According to the table, it will be .NET Standard 2.0 compliant in the vNext release. 根据该表,它将在vNext版本中符合.NET Standard 2.0标准。 However, it's target audience will not change. 但是,它的目标受众不会改变。

To directly answer your question, if you want to write an application with the widest possible, single deploy solution, you would want to use .NET Core . 要直接回答您的问题,如果您想使用尽可能广泛的单一部署解决方案编写应用程序,则需要使用.NET Core However, if you're using a library that is currently built upon .NET Framework 2.0 and 4.5, then you will be stuck using .NET Framework and having that separate UWP solution for that target. 但是,如果您使用的是当前基于.NET Framework 2.0和4.5构建的库,那么您将无法使用.NET Framework并为该目标使用单独的UWP解决方案。

If it provides something that you can call through a Web API call, you could have that running on your server in .NET Framework, and have a single solution in .NET Core to deploy to your end users. 如果它提供了可以通过Web API调用调用的内容,则可以在.NET Framework中的服务器上运行该调用,并在.NET Core中使用单个解决方案来部署到最终用户。 If it is integrated to your code, you're unfortunately out of luck until a .NET Core update is provided. 如果它已集成到您的代码中,那么在提供.NET Core更新之前,您很幸运。

Hopefully this has cleared up some of the confusion among the different technology names. 希望这可以消除不同技术名称之间的一些混淆。

EDIT 编辑

After some clarification on your specific situation, I can clear things up for you. 在对您的具体情况作出一些澄清后,我可以为您解决问题。 You can not make a single solution that will target both .NET Framework and .NET Core. 无法制作同时针对.NET Framework和.NET Core的单一解决方案。 The compile in completely different ways, with different underlying technology, so this is the same situation as trying to use your .NET 4.5 version in a .NET 2.0 solution. 使用不同的底层技术以完全不同的方式进行编译,因此这与尝试在.NET 2.0解决方案中使用.NET 4.5版本的情况相同。

However, there are tutorials out there to allow for you port your project to Core. 但是,有一些教程允许您将项目移植到Core。 For the most part, just copy the class bodies into your .NET Core solution, and most stuff will work correctly. 在大多数情况下,只需将类主体复制到.NET Core解决方案中,大多数东西都能正常工作。 There are some pieces that have been abandoned, some that haven't been completely 100% fleshed out yet (not for your case, but Entity Framework doesn't have all the same features for example). 有些部分已经被废弃,有些部分尚未完全100%充实(不适用于您的情况,但实体框架并不具备所有相同的功能)。 There are also some calls that have changed a little bit. 还有一些电话改变了一点点。

The good new is that moving forward, .NET Core will give you the broadest reach possible. 好的新东西是向前发展,.NET Core将为您提供最广泛的可能性。 .NET Framework is not going away, but it and Core will be much more in sync with each other. .NET Framework不会消失,但它和Core将更加同步。

The other benefit to .NET Core is that it uses an iterative approach to deployment so you won't be waiting 2 years for the next major upgrade. .NET Core的另一个好处是它使用迭代方法进行部署,因此您不会等待2年时间进行下一次重大升级。 With everything being delivered through NuGet, you'll have a much faster turn-around on improvements and new features. 通过NuGet提供的一切,您将在改进和新功能上实现更快的转变。

I'm assuming you've already read the Microsoft article with the nice table and everything is clear as mud now. 我假设你已经用漂亮的表读过微软的文章,现在一切都很清楚。 If so, you're in the same boat as I was before I spent the better part of an afternoon looking into this (and trying to port my reflection-heavy library to .NET Core, which I should mention is also my only .NET Core porting effort). 如果是这样的话,你和我之前在同一条船上度过了一个下午的大部分时间(并尝试将我的反思重型库移植到.NET Core,我应该提到它也是我唯一的 .NET)核心移植工作)。 What follows is not the official party line but my personal summary of what I found through lots of reading (and from being a .NET developer since .NET 1.0). 接下来的不是官方派对,而是我通过大量阅读(以及自.NET 1.0以来成为.NET开发人员)所发现的内容。 I cannot vouch for its total accuracy (especially when it comes to mobile development, of which I'm almost entirely ignorant) and corrections are certainly welcome. 我无法保证其完全准确性(特别是涉及移动开发时,我几乎完全无知)并且肯定会更正。 If there are lots I'll just make it wiki. 如果有很多我会把它做成维基。

I will walk through it more or less chronologically because I've found that just makes the most sense if you want to understand how the old and new relate. 我会或多或少地按时间顺序浏览它,因为我发现如果你想了解新旧有何关联,那就最有意义了。 It should probably be pared down a lot, but at present I lack the time to do so. 它可能应该减少很多,但目前我没有时间这样做。 There is a TL;DR version at the very end, though. 一个TL; DR版本在最后,虽然。

The long and winding road 漫长而曲折的道路

Despite its Java ancestry, .NET has never seriously attempted to be "write once, run anywhere". 尽管它具有Java系统,但.NET从未真正尝试过“一次编写,随处运行”。 It started off as very much in the camp of Windows, and even though it compiles to bytecode and did not go overboard with explicit Windowsisms and is thus theoretically very portable, that's not what MS was really interested in. Part of the .NET Framework was open sourced early on, and a bunch of open source enthousiasts picked it up and ran with it, giving us Mono . 它开始于Windows的阵营,即使它编译为字节码并且没有过度使用明确的Windows主义,因此理论上非常便携,这不是MS真正感兴趣的。部分.NET Framework是早期开源,一群开源的enthousiast选择了它并与它一起运行,给我们Mono Mono is important because it's the first alternate platform and library set for .NET and illustrates the ideas of platform versus library versus toolchain. Mono很重要,因为它是.NET的第一个备用平台库集 ,并说明了平台与库与工具链的思想。 Mono attempts to give a (more or less) complete implementation of the Common Language Runtime and it's associated Base Class Library. Mono尝试提供公共语言运行时的(或多或少)完整实现,以及它的关联基类库。 This is important: although Mono runs on Linux (and some other Unices) it is not a separate plaform in that it implements (some versions of) the CLR + BCL. 这很重要:虽然Mono在Linux(以及其他一些Unices)上运行,但它并不是一个单独的平台,因为它实现了CLR + BCL(某些版本)。 There are runtime differences (path names and the like) that matter to application developers, but for practical library programming, you can consider Mono and the .NET Framework for Windows "the same" platform with a slightly different implementation. 存在与应用程序开发人员相关的运行时差异(路径名称等),但对于实际的库编程,您可以将Mono和.NET Framework for Windows视为“相同”的平台,实现略有不同。 I stress this because we're going to encounter .NET code targeting Windows that is not running on the CLR and which is (ironically or otherwise) harder to port for. 我强调这一点是因为我们将遇到针对在CLR上运行的Windows的.NET代码,并且(具有讽刺意义或其他方面)难以移植。

Then came along Windows Phone (multiple versions), Windows CE (multiple versions), Windows Embedded CE, Windows Toaster (OK, that one doesn't really exist) and some flavor of .NET was basically reinvented every time -- still .NET but with fundamental stuff from the runtime and/or BCL missing or changed. 然后出现了Windows Phone(多个版本),Windows CE(多个版本),Windows Embedded CE,Windows Toaster(好的,那个实际上并不存在),并且每次基本上都重新发明了.NET的某些风格 - 仍然是.NET但是运行时和/或BCL的基本内容缺失或改变了。 Here's where we get .NET Compact , .NET Micro , Windows Phone (old style, no separate name for the framework) and Silverlight . 这是我们获得.NET Compact.NET MicroWindows Phone (旧样式,框架没有单独名称)和Silverlight的地方 All of these should be considered separate platform + library combos that resemble the .NET Framework enough to make cross-platform development possible, but differ from it enough to make it not that easy . 所有这些都应该被认为是类似于.NET Framework的独立平台+库组合,足以使跨平台开发成为可能,但不同于它足以使它变得不那么容易 As keeping track of what was supported where became most inconvenient for shared libraries, someone came up with the idea of Portable Class Libraries and their associated profiles (the collection of which is known as the .NET Portable Reference Assemblies ). 为了跟踪支持共享库最不方便的内容,有人提出了可移植类库及其相关配置文件(其集合称为.NET可移植参考组件 )的概念。

Basically, you target a set of specific combinations of .NET version, platform (and library), and you'd get reference assemblies that mimicked those combinations to compile against. 基本上,您定位一组.NET版本,平台(和库)的特定组合,并且您将获得模仿这些组合以进行编译的参考程序集。 Many different profiles exist depending on what flavors your explicitly wish to target. 根据您明确希望定位的口味,存在许多不同的配置文件。 This was a first attempt at what .NET Standard is now trying on a grander scale. 这是.NET Standard现在尝试更大规模的第一次尝试。 Basically, PCL is now obsolete unless you're targeting something .NET Standard isn't trying to support . 基本上, PCL现在已经过时了,除非你的目标是.NET Standard没有试图支持的东西 .NET Standard does away with the idea of a bazillion different profiles, which is good, at the expense of cutting some stuff your library could target earlier, which is bad. .NET标准不再考虑大量不同的配置文件,这样做很好,但却牺牲了你的库可以早先定位的一些东西,这很糟糕。 There are resources online to help with the transition from PCL to .NET Standard. 有在线资源可以帮助从PCL过渡到.NET Standard。 If you're looking at portable code now , you don't want to focus on PCL unless you really want to support some pretty marginal platforms (no offense intended to people still developing for them). 如果你现在正在寻找可移植的代码,你不想把重点放在PCL,除非你真的想支持一些漂亮的边际平台(无犯罪意图的人对他们仍处于发展阶段)。

Universal Windows Platform is, as the name implies, a platform. 顾名思义, 通用Windows平台是一个平台。 Specifically, it is the .NET Platform that's supported by Windows Store Apps (both on the desktop and on the phone). 具体来说,Windows Store应用程序(桌面和手机)都支持.NET平台。 That's it, no more, no less. 就是这样,不多也不少。 It's best considered as the natural successor to Silverlight in that it's a sandboxed framework support for both desktop and mobile. 它最好被认为是Silverlight的自然继承者,因为它是桌面和移动设备的沙盒框架支持。 Despite the name, it is not a universal platform and it is not what you want all your code to target. 尽管有这个名字,但它并不是一个通用的平台,它不是你想要的所有代码所针对的。 It is a platform you may want to enable for your code, and it is unique in that it is the only platform I know of that has two runtimes in the same version. 它是您可能希望为您的代码启用的平台,它的独特之处在于它是我所知道的唯一一个在同一版本中具有两个运行时的平台。 Coming right up! 马上就来!

.NET Native was not mentioned in the original post but often comes up in these discussions because it, too, is new, like .NET Core, and sounds very sexy because it compiles .NET directly to machine code (ahead of time compilation, not JIT compilation). 原始帖子中没有提到.NET Native ,但是经常出现在这些讨论中,因为它也是新的,比如.NET Core,听起来非常性感,因为它直接将.NET编译为机器代码(提前编译,而不是JIT编译)。 It is not a complete new platform but a new runtime for UWP apps (and only those) when compiled in Release mode. 在发布模式下编译时,它不是一个完整的新平台,而是UWP应用程序的新运行时(仅限于那些)。 In Debug mode, they use the CoreCLR (the .NET Core runtime). 在调试模式下,它们使用CoreCLR(.NET Core运行时)。 You won't need to think about this very hard unless you really want to build a UWP app, because there's all sorts of interesting stuff going on with reflection in .NET Native that needs separate attention from the app developer. 除非你真的想要构建一个UWP应用程序,否则你不需要考虑这个问题,因为.NET Native中的反射会发生各种有趣的事情需要应用程序开发人员的单独关注。

And now we come to .NET Core ! 现在我们来到.NET Core .NET Core started off as "ASP.NET Core", but people quickly realized it could be much bigger than that. .NET Core起初是“ASP.NET核心”,但人们很快意识到它可能比这更大。 .NET Core is a new runtime (CoreCLR) + library combo with explicit cross-platform support (as in cross-OS). .NET Core是一个新的运行时(CoreCLR)+库组合,具有明确的跨平台支持(如跨操作系统)。 Unlike the CLR + BCL combo, where there's a Windows version and a Unix version in the form of Mono, .NET Core is one codebase for all platforms (with the usual platform specific crunchy bits to support the fluffy portable layer above, of course). 与CLR + BCL组合不同,其中有Windows版本和Mono形式的Unix版本,.NET Core是所有平台的一个代码库(当然,通常平台特定的脆弱位支持蓬松的便携层) 。 What further confuses people is that .NET Core is also the name of a new toolchain/project type to support building .NET Core applications, where before we only had MSBuild. 令人困惑的是.NET Core 也是支持构建.NET Core应用程序的新工具链/项目类型的名称,之前我们只有MSBuild。 This was necessitated by there being no Visual Studio for Linux, but MS is already moving away from this "let's keep it simple and JSON" approach and moving back to one universal format for both .NET Framework and .NET Core (and it's going to be MSBuild, because there's much more resting on that). 这是必需的,因为没有Visual Studio for Linux,但MS 已经摆脱了“让我们保持简单和JSON”的方法,并回归到.NET Framework和.NET Core的一种通用格式(它将会是MSBuild,因为那里有更多的东西)。

.NET Core is for the most part very much compatible with the .NET Framework. .NET Core在很大程度上与.NET Framework非常兼容。 As a result, if your .NET Core application is actually running on the .NET Framework (on Windows), it can load assemblies that target the .NET Framework, not merely .NET Core. 因此,如果您的.NET Core应用程序实际上在.NET Framework上运行(在Windows上),它可以加载面向.NET Framework的程序集,而不仅仅是.NET Core。 This is an important source of confusion and unportable code: while you can build against and load those assemblies, they will make your code non-portable. 这是混淆和不可移植代码的重要来源:虽然您可以构建并加载这些程序集,但它们会使您的代码不可移植。 .NET Core itself won't stop you from doing this; .NET Core本身不会阻止你这样做; .NET Standard (coming right up) will, if you line up your declarations properly. 如果您正确排列声明,.NET Standard(即将推出)将会出现。

With me so far? 和我一起到目前为止? Good, because now we're ready to drop .NET Standard on your unsuspecting head. 很好,因为现在我们已经准备好将.NET Standard放在你不知情的头上了。 .NET Standard is not a platform (in the sense that you can't download and install it on a machine), it is not a library (but there are packages to support it for build purposes), it is a profile. .NET Standard不是一个平台(在某种意义上说你不能在机器上下载和安装它),它不是一个库(但是有一些软件包支持它用于构建目的),它是一个配置文件。 It's an attempt to standardize the library surface across a wide range of the things we've just discussed. 这是尝试在我们刚刚讨论的各种事物中标准化库表面。 The idea being, if your code targets .NET Standard XY, you only need to switch your build tools to "please give me .NET Standard XY" and when you build your assembly, you can be sure it will be usable for all the platforms covered by XY Hooray! 我们的想法是,如果您的代码以.NET Standard XY为目标,您只需要将构建工具切换为“请给我.NET标准XY”,并且在构建程序集时,您可以确保它可用于所有平台由XY万岁覆盖! The world is simple again! 世界又变得简单了!

Well, not yet it isn't. 好吧,还没有。 The problem is that .NET Core is in heavy development at the moment, meaning a lot of stuff is missing or different -- even quite fundamental stuff that may be naturally present in your .NET Framework codebase, like marking your exceptions Serializable and giving them a separate constructor for deserialization so they work nicely across AppDomain s. 问题是.NET Core目前处于大量开发阶段,这意味着很多东西都缺失或不同 - 甚至是.NET Framework代码库中可能存在的非常基本的东西,比如标记您的异常Serializable并给予它们用于反序列化的单独构造函数,以便它们可以在AppDomain很好地工作。 There are no AppDomain s in .NET Core, hence no serialization, hence those constructors will not compile. .NET Core中没有AppDomain ,因此没有序列化,因此这些构造函数将无法编译。 Even the Serializable attribute itself is missing in older versions of CoreCLR. 在旧版本的CoreCLR中,甚至缺少Serializable属性本身。 If your MSBuild project uses custom targets, too bad, the .NET Core toolchain has no support for that stuff at the moment (it may, in the future, when it's MSBuild again). 如果您的MSBuild项目使用自定义目标,那么.NET Core工具链目前不支持这些内容(将来可能再次使用MSBuild)。 You can rewrite, of course, but not reuse. 当然,您可以重写,但不能重复使用。 So while you can target .NET Standard, you may need two separate projects and/or some conditional compilation to get your .NET Standard assembly for two different platforms. 因此,虽然您可以定位.NET Standard,但您可能需要两个单独的项目和/或一些条件编译来为两个不同的平台获取.NET Standard程序集。 If you're lucky (or can compromise a bit), your library is simple enough that building it with .NET Core alone might suffice. 如果你很幸运(或者可以妥协一下),你的库很简单,只用.NET Core构建它就足够了。 Make no mistake, though: there are still multiple .NET platforms and they still have their differences, .NET Standard merely tries to make it easier to port. 毫无疑问:仍有多个.NET平台,它们仍然存在差异,.NET Standard只是试图让它更容易移植。 So far it's limited, but already doing a cleaner job than PCL. 到目前为止它有限,但已经比PCL做得更干净。

To sum up a bit: .NET Core and .NET Framework (and all their little cousins and nephews) are separate platforms both conceptually and by implementation. 总结一下:.NET Core和.NET Framework(以及他们所有的小表兄弟和侄子)在概念上和实现上都是独立的平台。 .NET Standard is a targeting profile that simplifies efforts needed to port code between them (but does not yet make it completely transparent). .NET Standard是一种定位配置文件,可简化在它们之间移植代码所需的工作(但尚未使其完全透明)。 PCL is the precursor to Standard that can be disregarded if you're progressive. PCL是标准的前身,如果你是进步的话可以被忽视。


TL;DR starts here (but is still TL) TL; DR从这里开始(但仍然是TL)

To finally answer your question, what should you do if you're a modern library developer and you want to target "as large an audience as possible"? 要最终回答您的问题,如果您是一名现代图书馆开发人员,并且希望“尽可能多的受众”,您应该怎么做? Well first of all, you must make this smaller if possible. 首先,如果可能的话,你必须让它变小。 What platforms will you explicitly support and test against? 您将明确支持和测试哪些平台? Do you really want to target .NET Compact on your XBox 360? 您真的想在XBox 360上使用.NET Compact吗? Windows Phone 7? Windows Phone 7? The Silverlight of eight years ago? 八年前的Silverlight? If you do, you probably can't get around the PCL, but most of us will have the luxury of being able to avoid that. 如果你这样做,你可能无法绕过PCL,但我们大多数人都会有幸能够避免这种情况。 If not: queue up a separate PCL build if your profile doesn't match anything in the .NET Standard. 如果不是:如果您的配置文件与.NET Standard中的任何内容都不匹配,则排队单独的PCL构建。

Is your library very simple (using no reflection, no async / await , no dependencies on bigger frameworks like WCF)? 你的库是非常简单的(不使用反射,没有async / await ,不依赖于像WCF这样的大型框架)? Then you may be able to target the cross section of .NET 2.0 and the lowest version of .NET Standard that has the framework dependencies you need. 然后,您可以定位.NET 2.0的横截面以及具有所需框架依赖性的最低版本的.NET Standard。 Don't be fooled, the lower versions of .NET Standard are really disappointingly limited in what they offer, but you have to pay some price for portability. 不要被愚弄,.NET Standard的较低版本在它们提供的产品中实际上是令人失望的,但是你必须付出一定的便携性价格。 There is no toolchain support for building both for .NET 2.0 and some .NET Standard version. 构建.NET 2.0和某些.NET Standard版本没有工具链支持。 You must build it twice and test it "everywhere", although the cross-section means you can be reasonably sure it will work if it compiles. 您必须构建它两次并“在任何地方”进行测试,尽管横截面意味着您可以合理地确定它在编译时能够正常工作。 The resulting library will support every single .NET platform that can load vanilla .NET 2.0 assemblies (which is nearly all of them, but notably not Micro and Compact) and .NET Core, and all without platform toggles. 生成的库将支持每个可以加载vanilla .NET 2.0程序集(几乎全部,但特别是不是Micro和Compact)和.NET Core的.NET平台,并且所有这些程序都没有平台切换。 Congrats, the world has never seen something so portable! 恭喜,世界上从未见过如此便携的东西!

Will your library use reflection? 你的图书馆会用反射吗? Then you probably can't get around rewriting the code to make it compile for .NET Core, because the reflection API changed a while back and your code may not have caught up yet (as there was no need if you kept targeting the full framework only). 那么你可能无法绕过重写代码以使其为.NET Core编译,因为反射API改变了一段时间后你的代码可能还没有赶上(因为没有必要,如果你继续瞄准完整的框架)只要)。 You will want to bump your targeted .NET Framework version to 4.5 in this case, as this is the .NET Framework version that is compatible with these changes. 在这种情况下,您需要将目标.NET Framework版本提升到4.5,因为这是与这些更改兼容的.NET Framework版本。 Here you start getting tooling support: you can target .NET Standard 1.1, which covers a subset of .NET 4.5. 在这里,您开始获得工具支持:您可以定位.NET Standard 1.1,它涵盖了.NET 4.5的一个子集 If you find this subset is not enough, you will again have to resort to building twice: for the full .NET Framework and for .NET Core. 如果您发现此子集是不够的,您将再次使用构建两次:对于完整的.NET Framework和.NET Core。 The reason being that .NET Core 1.0 supports "more" than .NET Framework 4.5, but there is as yet no version of the .NET Framework you can target that's on par with Core (that will be "vNext"). 原因是.NET Core 1.0比.NET Framework 4.5支持“更多”,但是目前还没有.NET Framework的版本可以与Core(即“vNext”)相提并论。 So if you don't want to restrict yourself to .NET Core only but also want to support those of us still building plain old 4.5 desktop apps and .NET Standard 1.1 isn't enough for you, you'll have to split. 因此,如果您不想仅限于.NET Core,而且还希望支持我们这些仍然构建普通的旧4.5桌面应用程序并且 .NET Standard 1.1对您来说还不够,那么您将不得不拆分。 The wrong thing to do is target 1.1 and then import Framework 4.5 only packages/assemblies, as this will get you the worst of both worlds in terms of portability! 错误的做法是目标1.1,然后只导入Framework 4.5包/程序集,因为这将使您在可移植性方面成为最糟糕的两个世界!

Will your library need some of the improvements/extensions over 4.5 that were introduced in 4.5.1 or later, or packages that are only available for higher .NET Standard versions? 您的库是否需要4.5.1或更高版本中引入的4.5以上的一些改进/扩展,或仅适用于更高.NET标准版本的软件包? Then target the appropriate higher .NET Standard version instead. 然后定位适当的更高的.NET标准版本。 Note that Microsoft no longer officially supports any 4.x lower than 4.5.2 . 请注意,Microsoft 不再正式支持低于4.5.2的任何4.x. This does not mean you shouldn't target those versions (go as low as you reasonably can), but it does mean that you have an argument for using nothing less than .NET Standard 1.2, and if you can demand 4.6, no less than 1.5. 并不意味着你不应该针对这些版本(尽可能的低,你可以合理的),但它确实意味着你有一个论据使用无外乎.NET 1.2的标准,如果你可以要求4.6,不小于1.5。 This is not burdensome on consumers (if you're willing and able to install 4.6, you're almost certainly willing and able to install 4.6.2) and makes your life easier. 这对消费者来说并不累赘(如果你愿意并且能够安装4.6,你几乎肯定愿意并且能够安装4.6.2)并且让你的生活更轻松。 Depending on your code you could get away with only a .NET Core build, but you probably don't want to, because the .NET Core build chain isn't stable yet and will move back to MSBuild (as mentioned before). 根据您的代码,您可以只使用.NET Core构建,但您可能不想这样做,因为.NET Core构建链尚未稳定并将返回到MSBuild(如前所述)。 No sense in ditching all your project files for JSON only to move back again later! 放弃JSON的所有项目文件只是为了以后再回来!

Does your library use any sort of conditional compilation? 您的库是否使用任何类型的条件编译? Beware, with the .NET Core toolchain, you get different predefined symbols . 请注意,使用.NET Core工具链,您可以获得不同的预定义符号 They're extra super annoying because they insist on (say) making a distinction between 4.5, 4.5.1 and 4.5.2, which is a pain if you want to cover "4.5 and beyond". 他们非常讨厌,因为他们坚持(比如说)区分4.5,4.5.1和4.5.2,如果你想要涵盖“4.5及以上”,那将是一种痛苦。 Nothing a careful build can't handle, but nevertheless something you need to take into account. 没有任何细心的构建无法处理,但您需要考虑的事情。

I'm not covering mobile builds here (Xamarin and older Phone versions) because, well, I know very little about those! 我不是在这里介绍移动版本(Xamarin和旧手机版本)因为,我对这些知之甚少! I imagine the story is much the same as building for both .NET Core and the .NET Framework, in that building once only works for simple libraries and libraries where you don't have to care about backwards compatibility, and needs (at least) two builds otherwise, but as I said way back in the beginning, corrections are welcome. 我想这个故事与构建.NET Core和.NET Framework非常相似,因为该构建只适用于简单的库和库,您不必关心向后兼容性,并且需要(至少)否则两个构建,但正如我在开始时说的那样,欢迎更正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM