We often see the following types of packages when referencing a class library on NuGet
This means that it can be used with. NETFramework or projects that use. NET core, respectively. Such a package can take into account different framework versions of the project, but it is convenient to simply publish a package on NuGet.
So how do you create a generic class library like this?
Start by using vs2017 to create a new standard library
Edit Csproj File
Edit the label tagetframework, add the marked content
Overloading the project and then building the project can find that two folders have been generated in the debug directory, stating that you have created a class library that can be used for multiple target frameworks, called multi-target libraries
This is not enough because the API provided by NET45 is different from the API provided by Netstandard, such as the way to get the path to the program's working folder
The method of using. NET framework4.5 is
string RootDir = AppDomain.CurrentDomain.BaseDirectory
Using the. NET Core
string rootdir = appcontext.basedirectory;
For such differentiated code we should use conditional compilation method to be compatible, the method is as follows
To view the compilation symbols for a project, create a property, right-click Project
You can see that the build symbol for the project is NET45, and our compatible code can write it
And you can switch between different frame versions in the navigation bar to debug
The final step is to generate a nupkg package that cannot be sent to NuGet, right click on the item
Find NuGet package files under build directory and publish to NuGet
Create libraries that can be used in both the. NET core and the. NET Framework and publish to NuGet