目標 FrameworkTarget 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. 您可以在專案檔中使用目標 Framework Moniker (TFM) 來指定目標 Framework。You specify the target framework in your project file using Target Framework Monikers (TFMs).

應用程式或程式庫可以將目標設為某個版本的 .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.iOS (例如 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) 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.

針對某些目標 Framework (例如 .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).

針對以套件為基礎的目標 Framework (例如 .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 套件,本身沒有任何內容,而是一份相依性 (其他專案) 清單。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.

最新目標 Framework 版本Latest target framework versions

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

目標 FrameworkTarget Framework LatestLatest
穩定版本Stable Version
Target Framework Moniker (TFM)Target Framework Moniker (TFM) 已實作Implemented
.NET Standard 版本.NET Standard Version
.NET Standard.NET Standard 2.02.0 netstandard2.0netstandard2.0 N/AN/A
.NET Core.NET Core 2.12.1 netcoreapp2.1netcoreapp2.1 2.02.0
.NET Framework.NET Framework 4.7.24.7.2 net472net472 2.02.0

支援的目標 Framework 版本Supported target framework versions

目標 Framework 通常會由 TFM 參考。A target framework is typically referenced by a TFM. 下表顯示 .NET Core SDK 和 NuGet 用戶端所支援的目標 Framework。The following table shows the target frameworks supported by the .NET Core 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 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
.NET Framework.NET Framework net11net11
net20net20
net35net35
net40net40
net403net403
net45net45
net451net451
net452net452
net46net46
net461net461
net462net462
net47net47
net471net471
net472net472
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]

如何指定目標 FrameworkHow to specify target frameworks

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

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.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 的 API (netstandard1.4) 以及 .NET Framework 的 API (net40net45) 為目標。The following library project file targets APIs of .NET Standard (netstandard1.4) and APIs of the .NET Framework (net40 and net45). 使用具有多個目標 Framework 的複數 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>

您可以在程式庫或應用程式中撰寫條件程式碼,為每個目標 Framework 進行編譯: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
    }
}

組建系統知道代表目標 Framework 的前置處理器符號,如支援的目標 Framework 版本表格中所示。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 目標 Framework 之前置處理器符號的完整清單如下:The complete list of preprocessor symbols for .NET Core target frameworks is:

目標 FrameworkTarget Frameworks 符號Symbols
.NET Framework.NET Framework NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472NET20, NET35, NET40, NET45, NET451, NET452, NET46, NET461, NET462, NET47, NET471, NET472
.NET Standard.NET Standard NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0NETSTANDARD1_0, NETSTANDARD1_1, NETSTANDARD1_2, NETSTANDARD1_3, NETSTANDARD1_4, NETSTANDARD1_5, NETSTANDARD1_6, NETSTANDARD2_0
.NET Core.NET Core NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2NETCOREAPP1_0, NETCOREAPP1_1, NETCOREAPP2_0, NETCOREAPP2_1, NETCOREAPP2_2

已被取代的目標 FrameworkDeprecated target frameworks

下列目標 Framework 已被取代。The following target frameworks are deprecated. 以這些目標 Framework 為目標的套件應該移轉至所指出的取代項目。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