Support multiple .NET Framework versions in your project file
Artikel
When you first create a project, we recommend you create a .NET Standard class library, as it provides compatibility with the widest range of consuming projects. By using .NET Standard, you add cross-platform support to a .NET library by default. However, in some scenarios, you may also need to include code that targets a particular framework. This article shows you how to do that for SDK-style projects.
For SDK-style projects, you can configure support for multiple targets frameworks (TFM) in your project file, then use dotnet pack or msbuild /t:pack to create the package.
Anteckning
nuget.exe CLI does not support packing SDK-style projects, so you should only use dotnet pack or msbuild /t:pack. We recommend that you include all the properties that are usually in the .nuspec file in the project file instead. To target multiple .NET Framework versions in a non-SDK-style project, see Supporting multiple .NET Framework versions.
Create a project that supports multiple .NET Framework versions
Create a new .NET Standard class library either in Visual Studio or use dotnet new classlib.
We recommend that you create a .NET Standard class library for best compatibility.
Edit the .csproj file to support the target frameworks. For example, change
Make sure that you change the XML element changed from singular to plural (add the "s" to both the open and close tags).
If you have any code that only works in one TFM, you can use #if NET45 or #if NETSTANDARD2_0 to separate TFM-dependent code. (For more information, see How to multitarget.) For example, you can use the following code:
C#
publicstring Platform {
get {
#if NET45return".NET Framework"#elif NETSTANDARD2_0return".NET Standard"#else#error This code block does not match csproj TargetFrameworks list#endif
}
}
Add any NuGet metadata you want to the .csproj as MSBuild properties.
If you want to separate build-related properties from NuGet metadata, you can use a different PropertyGroup, or put the NuGet properties in another file and use MSBuild's Import directive to include it. Directory.Build.Props and Directory.Build.Targets are also supported starting with MSBuild 15.0.
Now, use dotnet pack and the resulting .nupkg targets both .NET Standard 2.0 and .NET Framework 4.5.
Here is the .csproj file that is generated using the preceding steps and .NET Core SDK 2.2.
XML
<ProjectSdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFrameworks>netstandard2.0;net45</TargetFrameworks><Description>Sample project that targets multiple TFMs</Description></PropertyGroup></Project>
Crie um projeto .NET e aprenda a adicionar pacotes e gerenciar dependências de pacotes em seu projeto. Use a CLI do .NET Core e o registro NuGet para adicionar bibliotecas e ferramentas aos seus aplicativos C# por meio do Visual Studio Code.
Detalhes exatos sobre como especificar números de versão e intervalos para outros pacotes dos quais um pacote NuGet depende e como as dependências são instaladas.