简体   繁体   English

NET Standard 库可以引用另一个 NET Standard 库吗?

[英]Can a NET Standard library reference another NET Standard library?

I've inherited a bunch of net framework 4.71 solutions, and two net core 2.1 solutions.我继承了一堆 net framework 4.71 解决方案,以及两个 net core 2.1 解决方案。

I need to write a library for blob storage that can be used by both of them.我需要为他们两个都可以使用的 blob 存储编写一个库。 I found that the net framework won't call a net core library, and a net core framework won't call the net framework library.我发现net framework不会调用net core库,net core framework也不会调用net framework库。 So I was in the midst of trying a couple of net standard 2.1 libraries.所以我正在尝试几个网络标准 2.1 库。 (one for external models and one for the blob storage functions) (一个用于外部模型,一个用于 blob 存储功能)

I haven't even gotten to the point of making sure that the net core and net framework projects can reference net standard libraries.我什至还没有达到确保 net core 和 net framework 项目可以引用 net 标准库的地步。

Question 1) can net framework and net core reference net standard libraries?问题1)net framework和net core可以参考net标准库吗?

Question 2) is there a way to even share code between net core and net framework or should I just work on creating duplicate libraries?问题 2) 有没有办法在 net core 和 net framework 之间共享代码,或者我应该只创建重复的库?

Edit: I did just finally figure out what I was doing wrong in referencing the net standard model project from the blob storage net standard project.编辑:我终于弄清楚我在从 blob 存储网络标准项目中引用网络标准 model 项目时做错了什么。 I had to add the project reference directly.我不得不直接添加项目引用。 Haven't had to do that in a long time.好久没这么做了。 Visual Studio would add it for me with net framework. Visual Studio 会用网络框架为我添加它。

Edit2: looks like I'm just an idiot. Edit2:看起来我只是个白痴。 I finally figured out that I needed to also manually add the project reference for the library to the net core project.我终于发现我还需要手动将库的项目引用添加到 net core 项目中。 Now it compiles.现在它编译了。 Thanks everyone.感谢大家。

can net standard libraries reference other net standard libraries?网络标准库可以引用其他网络标准库吗?

yes;是的; a netstandard2.0 library can reference another netstandard2.0 library;一个 netstandard2.0 库可以引用另一个 netstandard2.0 库; a netstandard2.1 library can reference netstandard2.0 and netstandard2.1 libraries netstandard2.1 库可以引用 netstandard2.0 和 netstandard2.1 库

can net framework and net core reference net standard libraries can net framework 和 net core 参考 net 标准库

.NET Framework 4.7.1 can allegedly reference netstandard2.0 libraries, although it has some assembly-binding-redirect problems in a few areas ("unsafe", "buffers", etc) - and some areas are simply glitchy; .NET 框架 4.7.1据称可以引用 netstandard2.0 库,尽管它在一些领域(“不安全”、“缓冲区”等)存在一些装配绑定重定向问题 - 有些领域只是小故障; it cannot reference netstandard2.1 libraries它不能引用 netstandard2.1 库

.NET Core can reference netstandard libraries; .NET Core 可以参考netstandard库; these days you shouldn't really be looking at .NET Core below .NET Core 3.1, which means it can reference both netstandard2.0 and netstandard2.1 libraries这些天你不应该真的看 .NET Core 低于 .NET Core 3.1,这意味着它可以引用 netstandard2.0 和 netstandard2.1 库

is there a way to even share code between net core and net framework or should I just work on creating duplicate libraries有没有办法在网络核心和网络框架之间共享代码,或者我应该只致力于创建重复的库

multi-targeting is also an option, if netstandard doesn't work well for you;如果 netstandard 不适合您,多目标也是一种选择; you can use <TargetFrameworks>netcoreapp3.1;net471</TargetFrameworks> , for example, and use #if as necessary to switch between target-specific implementations.例如,您可以使用<TargetFrameworks>netcoreapp3.1;net471</TargetFrameworks> ,并根据需要使用#if在特定于目标的实现之间切换。

In additional to the excellent answer by Marc I would like to add the .net standard support matrix that shows what .net version support what .net standard version.除了 Marc 的出色回答之外,我还想添加.net 标准支持矩阵,该矩阵显示 .net 版本支持什么 .net 标准版本。

The way I understand it, the intended migration strategy is something like this据我了解,预期的迁移策略是这样的

  1. Migrate the core libraries to .net standard 2.0 (or lower), starting with the libraries with the fewest dependencies.将核心库迁移到 .net 标准 2.0(或更低版本),从依赖项最少的库开始。
  2. Migrate the applications, UI projects, and any other project that depends on things outside .net standard.迁移应用程序、UI 项目和任何其他依赖于 .net 标准之外的项目的项目。
  3. Migrate the core libraries to.Net core or.Net 5. No new versions of .net standard will be released .将核心库迁移到.Net core或.Net 5。 不会发布新版本的.net标准 So to use any new features the libraries need to be updated to .net core/5/6 etc.因此,要使用任何新功能,库需要更新为 .net core/5/6 等。

This way should limit the number of projects that need to be updated at once.这种方式应该限制需要一次更新的项目数量。 Allowing migration to be done piecemeal rather than all at once.允许零散地完成迁移,而不是一次性完成。

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

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