简体   繁体   中英

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.

I need to write a library for blob storage that can be used by both of them. 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. So I was in the midst of trying a couple of net standard 2.1 libraries. (one for external models and one for the blob storage functions)

I haven't even gotten to the point of making sure that the net core and net framework projects can reference net standard libraries.

Question 1) can net framework and net core reference net standard libraries?

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?

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. 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.

Edit2: looks like I'm just an idiot. I finally figured out that I needed to also manually add the project reference for the library to the net core project. 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; a netstandard2.1 library can reference netstandard2.0 and netstandard2.1 libraries

can net framework and net core reference net standard libraries

.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; it cannot reference netstandard2.1 libraries

.NET Core can reference netstandard libraries; 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

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; you can use <TargetFrameworks>netcoreapp3.1;net471</TargetFrameworks> , for example, and use #if as necessary to switch between target-specific implementations.

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.

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.
  2. Migrate the applications, UI projects, and any other project that depends on things outside .net standard.
  3. Migrate the core libraries to.Net core or.Net 5. No new versions of .net standard will be released . So to use any new features the libraries need to be updated to .net core/5/6 etc.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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