ChatGPT解决这个技术问题 Extra ChatGPT

Convert .NET Core 2.0 class libraries to .NET Standard

Is there a way to easily convert a class library targeting .NET Core 2.0 to .NET Standard?

If I understand it correctly, if one wants to maximize the reusability of class libraries in projects targeting different .NET frameworks e.g. .NET Framework, .NET Core, Xamarin, etc., it's a better idea to target .NET Standard -- provided that all the required APIs are available in the version of .NET Standard that will be targeted.

This is the reason why I want to convert my class libraries from .NET Core 2.0 to .NET Standard 1.6 or .NET Standard 2.0.


A
Artur A

In the project file, you can point target compilation to netstandard with the exact version.

Example of Proj.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>
</Project>
...

Microsoft provides good documentation about targeting types.

Dotnet Standard is not a framework or a library, it is an abstract set of instructions: what functionality should have System.Array, String, List, and so on. Currently, there are different implementations: .NET Framework, .NET Core, Mono, Xamarin, Windows Phone. It means that different implementations can recompile and reuse your library targeting netstandard. It is a very good choice for a NuGet package.

You can play with the versions and find the minimum function set required for your library. Each Dotnet Standard extends the functionality of the previous version. Thus, the less the targeted version is selected the more platforms your library will support.


While .Net Standard isn't a framework, you still set it in your project.csproj file's tag or in the project's Properties -> "Target Framework" drop down box. Naming stuff is hard!
M
Martin Ullrich

You can edit the csproj file (can be done inside VS by right-clicking on the project file) and change

<TargetFramework>netcoreapp2.0</TargetFramework>

to

<TargetFramework>netstandard2.0</TargetFramework>

Package Microsoft.AspNetCore.All 2.0.0 supports: netcoreapp2.0 (.NETCoreApp,Version=v2.0)
And after I remove the package reference, it says static main method not found
Well, it worked but it caused a lot more problems that were worse that the original warning :D. BTW I believe my .net core app was 2.1 rather than 2.0, i.e. "netcoreapp2.1".
It works when converting from .net standard to .net core
I did this and after this change the "Target Framework" in "Properies"is empty. Does this result in any negative conciquences?
B
Bùi Đức Khánh

Go to Project Folder Open .csproj file Replace your Target Framwork netcoreapp2.2 TO netstandard2.0

See this picture to more clear

https://i.stack.imgur.com/g1tMQ.png


M
Maximiliano Rios

My five cents on this topic. I had to convert libraries already written in .Net Core 3.1 and 5.0. When you open a solution or add the project to your current .Net Standard solution it won't show the list of .Net Standard options. The reason is you have 2 references to update in order to make it work. First, change the target framework in the .csproj file. For example, to switch to .Net Standard 2.1 I modified the file in the following way:

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>

Second, change the project type reference in the Solution (.sln file) opening it with any editor or it won't recognize it either. The GUID for .Net Standard 2.1 is 9A19103F-16F7-4668-BE54-9A1E7A4F7556. If you have a different version of standard the trick is adding a new project, check the GUID for the project type and replace it in the solution file.

EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = ...
EndProject

Any chance you can cite a reference for "The GUID for .Net Standard 2.1 is 9A19103F-16F7-4668-BE54-9A1E7A4F7556" I find conflicting information online and I can't find anything that seems to be definitive.