Target frameworks

https://docs.microsoft.com/en-us/dotnet/standard/frameworks

When you target a framework in an app or library, you're specifying the set of APIs that you'd like to make available to the app or library. You specify the target framework in your project file using Target Framework Monikers (TFMs).

An app or library can target a version of .NET Standard. .NET Standard versions represent standardized sets of APIs across all .NET implementations. For example, a library can target .NET Standard 1.6 and gain access to APIs that function across .NET Core and .NET Framework using the same codebase.

An app or library can also target a specific .NET implementation to gain access to implementation-specific APIs. For example, an app that targets Xamarin.iOS (for example, Xamarin.iOS10) gets access to Xamarin-provided iOS API wrappers for iOS 10, or an app that targets the Universal Windows Platform (UWP, uap10.0) has access to APIs that compile for devices that run Windows 10.

For some target frameworks (for example, the .NET Framework), the APIs are defined by the assemblies that the framework installs on a system and may include application framework APIs (for example, ASP.NET).

For package-based target frameworks (for example, .NET Standard and .NET Core), the APIs are defined by the packages included in the app or library. A metapackage is a NuGet package that has no content of its own but is a list of dependencies (other packages). A NuGet package-based target framework implicitly specifies a metapackage that references all the packages that together make up the framework.

Latest target framework versions

The following table defines the most common target frameworks, how they're referenced, and which version of the .NET Standard they implement. These target framework versions are the latest stable versions. Pre-release versions aren't shown. A Target Framework Moniker (TFM) is a standardized token format for specifying the target framework of a .NET app or library.

Target FrameworkLatest 
Stable Version
Target Framework Moniker (TFM)Implemented 
.NET Standard Version
.NET Standard2.0netstandard2.0N/A
.NET Core Application2.0netcoreapp2.02.0
.NET Framework4.7.2net4722.0

Supported target framework versions

A target framework is typically referenced by a TFM. The following table shows the target frameworks supported by the .NET Core SDK and the NuGet client. Equivalents are shown within brackets. For example, win81 is an equivalent TFM to netcore451.

Target FrameworkTFM
.NET Standardnetstandard1.0
netstandard1.1
netstandard1.2
netstandard1.3
netstandard1.4
netstandard1.5
netstandard1.6
netstandard2.0
.NET Corenetcoreapp1.0
netcoreapp1.1
netcoreapp2.0
netcoreapp2.1
.NET Frameworknet11
net20
net35
net40
net403
net45
net451
net452
net46
net461
net462
net47
net471
net472
Windows Storenetcore [netcore45]
netcore45 [win] [win8]
netcore451 [win81]
.NET Micro Frameworknetmf
Silverlightsl4
sl5
Windows Phonewp [wp7]
wp7
wp75
wp8
wp81
wpa81
Universal Windows Platformuap [uap10.0]
uap10.0 [win10] [netcore50]

How to specify target frameworks

Target frameworks are specified in your project file. When a single target framework is specified, use the TargetFramework element. The following console app project file demonstrates how to target .NET Core 2.0:

XMLCopy
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> </Project> 

When you specify multiple target frameworks, you may conditionally reference assemblies for each target framework. In your code, you can conditionally compile against those assemblies by using preprocessor symbols with if-then-else logic.

The following library project file targets APIs of .NET Standard (netstandard1.4) and APIs of the .NET Framework (net40 and net45). Use the plural TargetFrameworks element with multiple target frameworks. Note how the Condition attributes include implementation-specific packages when the library is compiled for the two .NET Framework TFMs:

XMLCopy
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup> <TargetFrameworks>netstandard1.4;net40;net45</TargetFrameworks> </PropertyGroup> <!-- Conditionally obtain references for the .NET Framework 4.0 target --> <ItemGroup Condition=" '$(TargetFramework)' == 'net40' "> <Reference Include="System.Net" /> </ItemGroup> <!-- Conditionally obtain references for the .NET Framework 4.5 target --> <ItemGroup Condition=" '$(TargetFramework)' == 'net45' "> <Reference Include="System.Net.Http" /> <Reference Include="System.Threading.Tasks" /> </ItemGroup> </Project> 

Within your library or app, you write conditional code to compile for each target framework:

C#Copy
public class MyClass
{
    static void Main() { #if NET40 Console.WriteLine("Target framework: .NET Framework 4.0"); #elif NET45 Console.WriteLine("Target framework: .NET Framework 4.5"); #else Console.WriteLine("Target framework: .NET Standard 1.4"); #endif } } 

The build system is aware of preprocessor symbols representing the target frameworks shown in the Supported target framework versions table. When using a symbol that represents a .NET Standard or .NET Core TFM, replace the dot with an underscore and change lowercase letters to uppercase (for example, the symbol for netstandard1.4 is NETSTANDARD1_4).

The complete list of preprocessor symbols for .NET Core target frameworks is:

Target FrameworksSymbols
.NET FrameworkNET20NET35NET40NET45NET451NET452NET46NET461NET462NET47NET471NET472
.NET StandardNETSTANDARD1_0NETSTANDARD1_1NETSTANDARD1_2NETSTANDARD1_3NETSTANDARD1_4NETSTANDARD1_5NETSTANDARD1_6NETSTANDARD2_0
.NET CoreNETCOREAPP1_0NETCOREAPP1_1NETCOREAPP2_0NETCOREAPP2_1

Deprecated target frameworks

The following target frameworks are deprecated. Packages targeting these target frameworks should migrate to the indicated replacements.

Deprecated TFMReplacement
aspnet50
aspnetcore50
dnxcore50
dnx
dnx45
dnx451
dnx452
netcoreapp
dotnet
dotnet50
dotnet51
dotnet52
dotnet53
dotnet54
dotnet55
dotnet56
netstandard
netcore50uap10.0
winnetcore45
win8netcore45
win81netcore451
win10uap10.0
winrtnetcore45

See also

Packages, Metapackages and Frameworks
Developing Libraries with Cross Platform Tools
.NET Standard
.NET Core Versioning
dotnet/standard GitHub repository
NuGet Tools GitHub Repository
Framework Profiles in .NET

转载于:https://www.cnblogs.com/liuqiyun/p/9122597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值