目标框架Target frameworks

以应用或库中的框架为目标时,需要指定想要向应用或库提供的 API 集。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. 使用目标框架名字对象 (TFM) 在项目文件中指定目标框架。You specify the target framework in your project file using Target Framework Monikers (TFMs).

应用或库可以使用 .NET Standard 版本作为目标。An app or library can target a version of .NET Standard. .NET Standard 版本表示所有 .NET 实现中的标准化 API 集。.NET Standard versions represent standardized sets of APIs across all .NET implementations. 例如,库可以使用 .NET Standard 1.6 作为目标,并获得对可使用相同基本代码跨 .NET Core 和 .NET Framework 工作的 API 的访问权限。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.

应用或库还能以一个特定 .NET 实现为目标,获得特定于实现的 API 的访问权限。An app or library can also target a specific .NET implementation to gain access to implementation-specific APIs. 例如,面向 Xamarin.iOS 的应用(如 Xamarin.iOS10)有权访问 Xamarin 提供的适用于 iOS 10 的 iOS API 包装器;面向通用 Windows 平台 (UWP) 的应用(如 uap10.0)有权访问为运行 Windows 10 的设备编译的 API。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.

对于某些目标框架(例如 .NET Framework),API 由框架在系统上安装的程序集定义,并且可能包括应用程序框架 API(例如 ASP.NET)。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).

对于基于包的目标框架(例如 .NET Standard 和 .NET Core),API 由包含在应用或库中的包定义。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. 元包是一个 NuGet 包,NuGet 包本身不包含任何内容,只是一个依赖项列表(其他包)。A metapackage is a NuGet package that has no content of its own but is a list of dependencies (other packages). 基于 NuGet 包的目标框架隐式指定一个元包,该元包引用一起构成框架的所有包。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

下表定义了最常见的目标框架、如何引用这些框架,以及它们实现的 .NET Standard 版本。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. 目标框架名字对象 (TFM) 是一个标准化令牌格式,用于指定 .NET 应用或库的目标框架。A Target Framework Moniker (TFM) is a standardized token format for specifying the target framework of a .NET app or library.

目标 FrameworkTarget Framework 最新Latest
稳定版本Stable Version
目标框架名字对象 (TFM)Target Framework Moniker (TFM) 已实现Implemented
.NET Standard 版本.NET Standard Version
.NET Standard.NET Standard 2.02.0 netstandard2.0netstandard2.0 不可用N/A
.NET Core.NET Core 2.22.2 netcoreapp2.2netcoreapp2.2 2.02.0
.NET Framework.NET Framework 4.84.8 net48net48 2.02.0

支持的目标框架版本Supported target framework versions

目标框架通常由 TFM 引用。A target framework is typically referenced by a TFM. 下表显示 .NET Core SDK 和 NuGet 客户端支持的目标框架。The following table shows the target frameworks supported by the .NET Core SDK and the NuGet client. 等效项显示在括号内。Equivalents are shown within brackets. 例如,win81 对于 netcore451 来说等效于 TFM。For example, win81 is an equivalent TFM to netcore451.

目标 FrameworkTarget Framework TFMTFM
.NET Standard.NET Standard netstandard1.0netstandard1.0
netstandard1.1netstandard1.1
netstandard1.2netstandard1.2
netstandard1.3netstandard1.3
netstandard1.4netstandard1.4
netstandard1.5netstandard1.5
netstandard1.6netstandard1.6
netstandard2.0netstandard2.0
.NET Core.NET Core netcoreapp1.0netcoreapp1.0
netcoreapp1.1netcoreapp1.1
netcoreapp2.0netcoreapp2.0
netcoreapp2.1netcoreapp2.1
netcoreapp2.2netcoreapp2.2
.NET Framework.NET Framework net11net11
net20net20
net35net35
net40net40
net403net403
net45net45
net451net451
net452net452
net46net46
net461net461
net462net462
net47net47
net471net471
net472net472
net48net48
Windows 应用商店Windows Store netcore [netcore45]netcore [netcore45]
netcore45 [win] [win8]netcore45 [win] [win8]
netcore451 [win81]netcore451 [win81]
.NET Micro Framework.NET Micro Framework netmfnetmf
SilverlightSilverlight sl4sl4
sl5sl5
Windows PhoneWindows Phone wp [wp7]wp [wp7]
wp7wp7
wp75wp75
wp8wp8
wp81wp81
wpa81wpa81
通用 Windows 平台Universal Windows Platform uap [uap10.0]uap [uap10.0]
uap10.0 [win10] [netcore50]uap10.0 [win10] [netcore50]

如何指定目标框架How to specify target frameworks

在项目文件中指定目标框架。Target frameworks are specified in your project file. 指定单个目标框架时,使用 TargetFramework 元素。When a single target framework is specified, use the TargetFramework element. 以下控制台应用项目文件演示了如何以 .NET Core 2.2 为目标:The following console app project file demonstrates how to target .NET Core 2.2:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

</Project>

指定多个目标框架时,可有条件地为每个目标框架引用程序集。When you specify multiple target frameworks, you may conditionally reference assemblies for each target framework. 在代码中,可使用具有 -if-then-else 逻辑的预处理器符号,有条件地针对这些程序集进行编译。In your code, you can conditionally compile against those assemblies by using preprocessor symbols with if-then-else logic.

以下库项目文件以 .NET Standard (netstandard1.4) 的 API 和 .NET Framework(net40net45)的 API 作为目标。The following library project file targets APIs of .NET Standard (netstandard1.4) and APIs of the .NET Framework (net40 and net45). 将复数形式的 TargetFrameworks 元素与多个目标框架一起使用。Use the plural TargetFrameworks element with multiple target frameworks. 请注意为两个 .NET Framework TFM 编译库时,Condition 属性包括特定于实现的包的方法:Note how the Condition attributes include implementation-specific packages when the library is compiled for the two .NET Framework TFMs:

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

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. 使用表示 .NET Standard 或 .NET Core TFM 的符号时,用下划线替代句点,并将小写字母转换为大写字母(例如,netstandard1.4 的符号是 NETSTANDARD1_4)。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).

完整的 .NET Core 目标框架的预处理器符号列表:The complete list of preprocessor symbols for .NET Core target frameworks is:

目标框架Target Frameworks 符号Symbols
.NET Framework.NET Framework NETFRAMEWORK, NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472, NET48NETFRAMEWORK, NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472, NET48
.NET Standard.NET Standard NETSTANDARD, NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0NETSTANDARD, NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0
.NET Core.NET Core NETCOREAPP, NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2NETCOREAPP, NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2

已弃用的目标框架Deprecated target frameworks

以下目标框架已弃用。The following target frameworks are deprecated. 以这些目标框架为目标的包应迁移到指明的替代框架。Packages targeting these target frameworks should migrate to the indicated replacements.

已弃用的 TFMDeprecated TFM ReplacementReplacement
aspnet50aspnet50
aspnetcore50aspnetcore50
dnxcore50dnxcore50
dnxdnx
dnx45dnx45
dnx451dnx451
dnx452dnx452
netcoreappnetcoreapp
dotnetdotnet
dotnet50dotnet50
dotnet51dotnet51
dotnet52dotnet52
dotnet53dotnet53
dotnet54dotnet54
dotnet55dotnet55
dotnet56dotnet56
netstandardnetstandard
netcore50netcore50 uap10.0uap10.0
winwin netcore45netcore45
win8win8 netcore45netcore45
win81win81 netcore451netcore451
win10win10 uap10.0uap10.0
winrtwinrt netcore45netcore45

请参阅See also