SDK 樣式專案中的目標 frameworkTarget frameworks in SDK-style projects

當您以應用程式或程式庫中的架構為目標時,您將指定要提供給應用程式或程式庫的一組 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. 您可以使用目標 framework 標記 (TFM) ,在專案檔中指定目標 framework。You specify the target framework in your project file using a target framework moniker (TFM).

應用程式或程式庫可以將目標設為某個版本的 .NET StandardAn 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 為目標的應用程式 (例如, Xamarin.iOS10) 可存取適用于 iOS 10 的 xamarin 提供的 IOS API 包裝函式,或是以通用 Windows 平臺 (UWP 為目標的應用程式, uap10.0) 可以存取針對執行 Windows 10 的裝置進行編譯的 api。For example, an app that targets Xamarin.iOS (for example, Xamarin.iOS10) has access to Xamarin-provided iOS API wrappers for iOS 10, or an app that targets Universal Windows Platform (UWP, uap10.0) has access to APIs that compile for devices that run Windows 10.

針對某些目標 framework (例如 .NET Framework),Api 是由架構安裝在系統上的元件所定義,而且可能包含應用程式架構 Api (例如 ASP.NET) 。For some target frameworks, such as .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).

針對以套件為基礎的目標 framework (例如,.NET 5、.NET Core 和 .NET Standard) ,Api 是由應用程式或程式庫中包含的套件所定義。For package-based target frameworks (for example, .NET 5, .NET Core, and .NET Standard), the APIs are defined by the packages included in the app or library. 「中繼套件」是 NuGet 套件,本身沒有任何內容,而是一份相依性 (其他專案) 清單。A metapackage is a NuGet package that has no content of its own but is a list of dependencies (other packages). 以 NuGet 套件為基礎的目標 Framework 會隱含指定一個中繼套件,該套件會參考組成架構的所有套件。A NuGet package-based target framework implicitly specifies a metapackage that references all the packages that together make up the framework.

最新版本Latest versions

下表定義最常見的目標 framework、其參考方式,以及它們所執行的 .NET Standard 版本。The following table defines the most common target frameworks, how they're referenced, and which version of .NET Standard they implement. 這些目標 Framework 版本是最新穩定版本。These target framework versions are the latest stable versions. 不顯示發行前版本。Pre-release versions aren't shown. 目標 framework 標記 (TFM) 是標準化的標記格式,用於指定 .NET 應用程式或程式庫的目標 framework。A target framework moniker (TFM) is a standardized token format for specifying the target framework of a .NET app or library.

目標架構Target framework LatestLatest
穩定版本stable version
目標 framework 標記 (TFM) Target framework moniker (TFM) 已實作Implemented
.NET Standard 版本.NET Standard version
.NET 5.NET 5 5.05.0 net 5。0net5.0 不適用N/A
.NET Standard.NET Standard 2.12.1 netstandard 2。1netstandard2.1 不適用N/A
.NET Core.NET Core 3.13.1 netcoreapp 3。1netcoreapp3.1 2.12.1
.NET Framework.NET Framework 4.84.8 net48net48 2.02.0

支援的目標 frameworkSupported target frameworks

目標 Framework 通常會由 TFM 參考。A target framework is typically referenced by a TFM. 下表顯示 .NET SDK 和 NuGet 用戶端支援的目標 framework。The following table shows the target frameworks supported by the .NET SDK and the NuGet client. 對等項目會顯示在括弧內。Equivalents are shown within brackets. 例如,win81netcore451 的對等 TFM。For example, win81 is an equivalent TFM to netcore451.

目標 FrameworkTarget Framework TFMTFM
.NET 5 (和 .NET Core) .NET 5 (and .NET Core) netcoreapp1.0netcoreapp1.0
netcoreapp1.1netcoreapp1.1
netcoreapp2.0netcoreapp2.0
netcoreapp2.1netcoreapp2.1
netcoreapp2.2netcoreapp2.2
netcoreapp 3。0netcoreapp3.0
netcoreapp 3。1netcoreapp3.1
net 5.0 *net5.0*
.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
netstandard 2。1netstandard2.1
.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]

* .NET 5.0 和更新版本的 Tfm 包含作業系統特定的變化。* .NET 5.0 and later TFMs include operating system-specific variations. 如需詳細資訊,請參閱下一節: .net 5 作業系統特定 tfmFor more information, see the following section, .NET 5 OS-specific TFMs.

.NET 5 作業系統專用 Tfm.NET 5 OS-specific TFMs

例如,針對每個 .NET 5.0 和更新版本的 TFM, net5.0 都有 TFM 的變化,其中包含作業系統特定的系結。For each .NET 5.0 and later TFM, for example, net5.0, there are TFM variations that include OS-specific bindings. 下表顯示這些變化。These variations are shown in the following table.

作業系統特定格式OS-specific format 範例Example
<base-tfm>-android<base-tfm>-android net 5.0-androidnet5.0-android
<base-tfm>-ios<base-tfm>-ios net 5.0-iosnet5.0-ios
<base-tfm>-macos<base-tfm>-macos net 5.0-macosnet5.0-macos
<base-tfm>-tvos<base-tfm>-tvos net 5.0-tvosnet5.0-tvos
<base-tfm>-watchos<base-tfm>-watchos net 5.0-watchosnet5.0-watchos
<base-tfm>>microsoft-windows-powershell<base-tfm>-windows net 5.0-windowsnet5.0-windows

net5.0TFM 僅包含可跨平臺運作的技術。The net5.0 TFM only includes technologies that work cross-platform. 指定作業系統特定的 TFM,可讓您的應用程式使用的作業系統專用 Api,例如 Windows Forms 或 iOS 系結。Specifying an OS-specific TFM makes APIs that are specific to an operating system available to your app, for example, Windows Forms or iOS bindings. 作業系統專用的 Tfm 也會繼承 TFM 可用的每個 API net5.0OS-specific TFMs also inherit every API available to the net5.0 TFM.

若要讓您的應用程式可在不同平臺上使用,您可以將目標設為多個 OS 專用 Tfm,並使用預處理器指示詞,在 OS 特定 API 呼叫周圍新增平臺防護 #ifTo make your app portable across different platforms, you can target multiple OS-specific TFMs and add platform guards around OS-specific API calls using #if preprocessor directives.

下表顯示 .NET 5 Tfm 與舊版 .NET 版本 Tfm 的相容性。The following table shows the compatibility of the .NET 5 TFMs with TFMs for previous .NET versions.

TFMTFM 相容于Compatible with 注意Notes
net 5。0net5.0 net1..具有 NU1701 警告的 4 () net1..4 (with NU1701 warning)
netcoreapp1..3.1 (參考 WinForms 或 WPF 時發出警告) netcoreapp1..3.1 (warning when WinForms or WPF is referenced)
netstandard1..2.1netstandard1..2.1
net 5.0-androidnet5.0-android xamarin. android (再加上繼承自) 的其他專案 net5.0xamarin.android (plus everything else inherited from net5.0)
net 5.0-iosnet5.0-ios xamarin (再加上繼承自) 的其他所有專案 net5.0xamarin.ios (plus everything else inherited from net5.0)
net 5.0-macosnet5.0-macos xamarin (再加上繼承自) 的其他所有專案 net5.0xamarin.mac (plus everything else inherited from net5.0)
net 5.0-tvosnet5.0-tvos xamarin. tvos (再加上繼承自) 的其他專案 net5.0xamarin.tvos (plus everything else inherited from net5.0)
net 5.0-watchosnet5.0-watchos xamarin. watchos (再加上繼承自) 的其他專案 net5.0xamarin.watchos (plus everything else inherited from net5.0)
net 5.0-windowsnet5.0-windows netcoreapp1..3.1 (以及繼承自) 的其他專案 net5.0netcoreapp1..3.1 (plus everything else inherited from net5.0) 包含 WinForms、WPF 和 UWP Api。Includes WinForms, WPF, and UWP APIs.
如需詳細資訊,請參閱 在桌面應用程式中呼叫 Windows 執行階段 apiFor information, see Call Windows Runtime APIs in desktop apps.

建議的目標Suggested targets

使用這些指導方針來判斷要在您的應用程式中使用的 TFM:Use these guidelines to determine which TFM to use in your app:

  • 可移植至多個平臺的應用程式應該以目標 net5.0Apps that are portable to multiple platforms should target net5.0. 這包括大部分的程式庫,但也 ASP.NET Core 和 Entity Framework。This includes most libraries but also ASP.NET Core and Entity Framework.

  • 平臺特定程式庫應以平臺特定的類別為目標。Platform-specific libraries should target platform-specific flavors. 例如,WinForms 和 WPF 專案應設為目標 net5.0-windowsFor example, WinForms and WPF projects should target net5.0-windows.

  • 跨平臺應用程式模型 (Xamarin 表單、ASP.NET Core) 和橋接器套件 (Xamarin Essentials) 至少應 net5.0 為目標,但可能也會以其他平臺特定的類別為目標,以加速更多 api 或功能。Cross-platform application models (Xamarin Forms, ASP.NET Core) and bridge packs (Xamarin Essentials) should at least target net5.0, but might also target additional platform-specific flavors to light-up more APIs or features.

Tfm 中的作業系統版本OS version in TFMs

您也可以在 TFM 結尾指定選用的 OS 版本,例如, net5.0-ios13.0 指出您的應用程式可使用的 api。You can also specify an optional OS version at the end of the TFM, for example, net5.0-ios13.0, which indicates what APIs are available to your app. (.NET 5 SDK 將會更新,以在發行時支援較新的 OS 版本。 ) 若要取得新發行的 Api,請將 TFM 中的作業系統版本遞增。(The .NET 5 SDK will be updated to include support for newer OS versions as they are released.) To gain access to newly released APIs, increment the OS version in the TFM. 您仍然可以將應用程式與舊版作業系統相容 (,並將專案新增至您的專案檔,以新增對較新版本) Api 呼叫的防護 SupportedOSPlatformVersionYou can still make your app compatible with earlier OS versions (and add guards around calls to later-version APIs) by adding the SupportedOSPlatformVersion element to your project file. SupportedOSPlatformVersion元素表示執行您的應用程式所需的最低作業系統版本。The SupportedOSPlatformVersion element indicates the minimum OS version required to run your app.

例如,下列專案檔摘錄會指定 iOS 14 Api 可供應用程式使用,但它可以在 iOS 13 或更新版本的電腦上執行。For example, the following project file excerpt specifies that iOS 14 APIs are available to the app, but it can run on iOS 13 or later machines.

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

  <PropertyGroup>
    <TargetFramework>net5.0-ios14.0</TargetFramework>
    <SupportedOSPlatformVersion>13.0</SupportedOSPlatformVersion> (minimum os platform version)
  </PropertyGroup>

  ...

</Project>

如何指定目標 frameworkHow to specify a target framework

目標 framework 是在專案檔中指定。Target frameworks are specified in a project file. 當指定單一目標 framework 時,請使用 TargetFramework 元素When a single target framework is specified, use the TargetFramework element. 下列主控台應用程式專案檔會示範如何將目標設為 .NET 5.0:The following console app project file demonstrates how to target .NET 5.0:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>

當您指定多個目標 Framework 時,您可以有條件地參考每個目標 Framework 的組件。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) 和 .NET Framework (net40net45) 的 api。The following library project targets APIs of .NET Standard (netstandard1.4) and .NET Framework (net40 and net45). 使用具有多個目標 framework 的複數 TargetFrameworks 元素Use the plural TargetFrameworks element with multiple target frameworks. Condition當您針對兩個 .NET Framework tfm 編譯程式庫時,屬性會包含特定的執行套件: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>

在您的程式庫或應用程式中,您可以使用 預處理器 指示詞,針對每個目標 framework 編譯條件式程式碼:Within your library or app, you write conditional code using preprocessor directives 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
    }
}

當您使用 SDK 樣式專案時,組建系統會注意代表支援的 目標 framework 版本 資料表中所顯示之目標 framework 的預處理器符號。The build system is aware of preprocessor symbols representing the target frameworks shown in the Supported target framework versions table when you're using SDK-style projects. 使用代表 .NET Standard、.NET Core 或 .NET 5 TFM 的符號時,以底線取代點和連字號,並將小寫字母變更為大寫 (例如,的符號 netstandard1.4NETSTANDARD1_4) 。When using a symbol that represents a .NET Standard, .NET Core, or .NET 5 TFM, replace dots and hyphens with an underscore and change lowercase letters to uppercase (for example, the symbol for netstandard1.4 is NETSTANDARD1_4).

.NET 目標 framework 的預處理器符號完整清單如下:The complete list of preprocessor symbols for .NET target frameworks is:

目標 FrameworkTarget Frameworks 符號Symbols
.NET Framework.NET Framework NETFRAMEWORK, NET48, NET472, NET471, NET47, NET462, NET461, NET46, NET452, NET451, NET45, NET40, NET35, NET20NETFRAMEWORK, NET48, NET472, NET471, NET47, NET462, NET461, NET46, NET452, NET451, NET45, NET40, NET35, NET20
.NET Standard.NET Standard NETSTANDARD, NETSTANDARD2_1, NETSTANDARD2_0, NETSTANDARD1_6, NETSTANDARD1_5, NETSTANDARD1_4, NETSTANDARD1_3, NETSTANDARD1_2, NETSTANDARD1_1, NETSTANDARD1_0NETSTANDARD, NETSTANDARD2_1, NETSTANDARD2_0, NETSTANDARD1_6, NETSTANDARD1_5, NETSTANDARD1_4, NETSTANDARD1_3, NETSTANDARD1_2, NETSTANDARD1_1, NETSTANDARD1_0
.NET 5 (和 .NET Core) .NET 5 (and .NET Core) NET5_0, NETCOREAPP, NETCOREAPP3_1, NETCOREAPP3_0, NETCOREAPP2_2, NETCOREAPP2_1, NETCOREAPP2_0, NETCOREAPP1_1, NETCOREAPP1_0NET5_0, NETCOREAPP, NETCOREAPP3_1, NETCOREAPP3_0, NETCOREAPP2_2, NETCOREAPP2_1, NETCOREAPP2_0, NETCOREAPP1_1, NETCOREAPP1_0

已被取代的目標 FrameworkDeprecated target frameworks

下列目標 Framework 已被取代。The following target frameworks are deprecated. 以這些目標 framework 為目標的套件應該遷移至指示的取代。Packages that target these target frameworks should migrate to the indicated replacements.

已被取代的 TFMDeprecated TFM 取代Replacement
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