[英]Can I use .NET Standard to wrap .NET Core NuGet packages and make them available in classic .NET?
I am going through this blog post:我正在浏览这篇博文:
https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx https://www.hanselman.com/blog/ReferencingNETStandardAssembliesFromBothNETCoreAndNETFramework.aspx
.. and its source code: .. 及其源代码:
https://github.com/ScarlettCode/Example https://github.com/ScarlettCode/Example
To my understanding, it wraps a .NET Core NuGet package (Microsoft.EntityFrameworkCore) in a .NET Standard library and makes it available for a classic .NET 4.6.1 application:据我了解,它将 .NET Core NuGet 包 (Microsoft.EntityFrameworkCore) 包装在 .NET Standard 库中,并使其可用于经典的 .NET 4.6.1 应用程序:
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />
Am I correct?我对么? Could that be a technique to wrap .NET Core NuGet packages in a .NET standard library can call them in classic .NET Framework 4.6.1 applications?这是一种将 .NET Core NuGet 包包装在 .NET 标准库中的技术,可以在经典的 .NET Framework 4.6.1 应用程序中调用它们吗?
Can I use .NET Standard to wrap .NET Core NuGet packages and make them available in classic .NET?我可以使用 .NET Standard 来包装 .NET Core NuGet 包并使其在经典 .NET 中可用吗?
Well, almost, but not quite.嗯,差不多,但不完全。
To my understanding, it wraps a .NET Core NuGet package (Microsoft.EntityFrameworkCore) in a .NET Standard library and makes it available for a classic .NET 4.6.1 application.据我了解,它将 .NET Core NuGet 包 (Microsoft.EntityFrameworkCore) 包装在 .NET Standard 库中,并使其可用于经典的 .NET 4.6.1 应用程序。
This is where you've misunderstood.这就是你误会的地方。 The package reference is for version 2.0.0 of Microsoft.EntityFrameworkCore.InMemory.包参考适用于 Microsoft.EntityFrameworkCore.InMemory 的 2.0.0 版。 We might consider this package to be "part of .NET Core" but in fact at a binary level the package targets .NETStandard 2.0.我们可能认为这个包是“.NET Core 的一部分”,但实际上在二进制级别,该包面向 .NETStandard 2.0。
How do I know the package targets .NET Standard 2.0?我怎么知道包的目标是 .NET Standard 2.0? I establish that by looking at the Dependencies section of its NuGet page or by consulting Fuget .我通过查看其NuGet 页面的 Dependencies 部分或咨询Fuget 来确定这一点。
.NET 4.6.1* and .NET Core 2.0 both support .NET Standard 2.0 , that is to say both of those frameworks (and newer versions) can reference .NET Standard 2.0 libraries. .NET 4.6.1* 和 .NET Core 2.0 都支持 .NET Standard 2.0 ,也就是说这两个框架(和更新版本)都可以引用 .NET Standard 2.0 库。
So let's go back to your original question:那么让我们回到你最初的问题:
Can I use .NET Standard to wrap .NET Core NuGet packages and make them available in classic .NET?我可以使用 .NET Standard 来包装 .NET Core NuGet 包并使其在经典 .NET 中可用吗?
No, but you can package your code up in libraries which target .NET Standard, and use those libraries from .NET Core projects AND .NET Framework projects;不,但您可以将代码打包到面向 .NET Standard 的库中,并使用来自 .NET Core 项目和 .NET Framework 项目的库; and likewise you can reference Nuget packages in .NET Framework projects provided they target .NET Standard even if the package has 'Core' in the name and you think of it as being part of .NET Core .同样,您可以在 .NET Framework 项目中引用 Nuget 包,前提是它们以 .NET Standard 为目标,即使包的名称中有“Core”并且您认为它是 .NET Core 的一部分。 This is the beauty of .NET Standard.这就是 .NET Standard 的美妙之处。
The key takeaway here is to pay close attention to the Dependencies section of Nuget description pages!这里的关键是密切关注 Nuget 描述页面的依赖关系部分!
*Note that whilst .NET 4.6.1 officially supports .NET Standard 2.0, Microsoft recommend using 4.7.2 or higher. *请注意,虽然 .NET 4.6.1 正式支持 .NET Standard 2.0,但 Microsoft 建议使用 4.7.2 或更高版本。 See my answer here for more information.请在此处查看我的回答以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.