随着 netstandard 2.0 和 netcore 2.0 的最终发布,为什么我们关心构建 netstandard2 库,同时我们可以使用完整的框架 4.6.1

With the final release of netstandard 2.0 and netcore 2.0, why we care to build netstandard2 library while we can use the full framework 4.6.1

.Netstandard2 与 .Net Core 2.0 和 vs2017.3 一起发布,带有 nuget4.3 和 API 表面覆盖 32k(除了 41 class)和全覆盖 net461

引用自Announcing .NET Core 2.0

You can now reference .NET Framework libraries from .NET Standard libraries using Visual Studio 2017 15.3. This feature helps you migrate .NET Framework code to .NET Standard or .NET Core over time (start with binaries and then move to source). It is also useful in the case that the source code is no longer accessible or is lost for a .NET Framework library, enabling it to be still be used in new scenarios.

We expect that this feature will be used most commonly from .NET Standard libraries. It also works for .NET Core apps and libraries. They can depend on .NET Framework libraries, too.

The supported scenario is referencing a .NET Framework library that happens to only use types within the .NET Standard API set. Also, it is only supported for libraries that target .NET Framework 4.6.1 or earlier (even .NET Framework 1.0 is fine).

因此,在 .netcore2 环境中,我们可以继续 build/use 完整的 .Net 框架,就像我们多年来所做的那样,而无需切换到 .netstandard2 库。

通过多目标项目,(net64;netstandard2),我们可以免费获得 .net standard 2.0(零时间),覆盖范围相同 API。

您能否分享您的经验:我们能否继续构建 Full Framework 4.6.1 class 库并在 .netcore2 中使用它?限制是什么?

构建 .NET Framework 库时,只能将 4.6.1 与 .NET Standard 结合使用。如果您想使用更新的 API,例如netstandard2.0 或 .NET 4.7,您需要将您的库重新定位到 4.7,然后 netstandard2.0 / netcoreapp2.0 项目不能使用它。实际上,您被锁定在 .NET Framework 版本 4.6.1 中并且永远不能(/应该)将其更新到更新的版本。 (当然,有一个解决方法涉及禁用隐式回退定义并在消费项目中手动设置 AssetTargetFallback,但它需要手动编辑 .csproj 文件并指示所有包消费者这样做)。

此外,兼容层无法防止使用未在 netstandard 上实现的 API,因此使用项目可能会出现有关缺少类型、方法、程序集等的异常。仅当您以 .NET Standard 为目标时,您的库保证可以在符合 .NET Standard 的平台上运行。

目前的实际限制是无法从 .NET Core 引用 .NET Framework 库项目(!)(不是 NuGet 包)/仅适用于特定设置和解决方法(您将收到不兼容的目标框架警告) .