Framework 目標概觀Framework targeting overview

在 Visual Studio 中,您可以指定要讓專案設為目標的 .NET 版本。In Visual Studio, you can specify the version of .NET that you want your project to target. Framework 目標有助於確保應用程式只使用指定 Framework 版本中可供使用的功能。Framework targeting helps guarantee that the application uses only functionality that is available in the specified framework version. 若要讓 .NET Framework 應用程式在另一部電腦上執行,該應用程式的目標 Framework 版本必須與該電腦上所安裝的 Framework 版本相容。For .NET Framework apps to run on another computer, the framework version that the application targets must be compatible with the framework version that's installed on the computer.

Visual Studio 解決方案可以包含以不同 .NET 版本為目標的專案。A Visual Studio solution can contain projects that target different versions of .NET. 不過,請注意,您只能使用單一組建的參考條件,或針對每個版本以遞迴方式建立不同的二進位檔,以建立單一版本的 .NET。However, note that you can only build against a single version of .NET either using reference conditionals for a single build or recursively build different binaries for each version. 如需目標 Framework 的詳細資訊,請參閱目標 FrameworkFor more information about target frameworks, see Target frameworks.

提示

您也可以針對不同平台的應用程式。You can also target applications for different platforms. 如需詳細資訊,請參閱多目標For more information, see Multitargeting.

Framework 目標功能Framework targeting features

Framework 目標包括下列功能:Framework targeting includes the following features:

  • 當您開啟以舊版 Framework 為目標的專案時,Visual Studio 可以自動將該專案升級,或是保留其目標。When you open a project that targets an earlier framework version, Visual Studio can automatically upgrade the project or leave the target as-is.

  • 當您建立 .NET Framework 專案時,您可以指定想要作為目標的 .NET Framework 版本。When you create a .NET Framework project, you can specify the version of the .NET Framework that you want to target.

  • 您可以在單一專案中以多個 Framework 為目標You can target multiple frameworks in a single project.

  • 在相同方案中,您可以讓數個不同的專案個別以不同版本的 .NET 為目標。You can target a different version of .NET in each of several projects in the same solution.

  • 您可以變更現有專案的目標 .NET 版本。You can change the version of .NET that an existing project targets.

    當您變更專案的目標 .NET 版本時,Visual Studio 會對參考和設定檔進行必要的變更。When you change the version of .NET that a project targets, Visual Studio makes any required changes to references and configuration files.

當您使用以舊版 Framework 為目標的專案時,Visual Studio 會動態地變更開發環境,如下所示:When you work on a project that targets an earlier framework version, Visual Studio dynamically changes the development environment, as follows:

  • 它會篩選 [新增新項目] 對話方塊、[新增參考] 對話方塊,以及 [新增服務參考] 對話方塊中的項目,以省略目標版本中未提供的選項。It filters items in the Add New Item dialog box, the Add New Reference dialog box, and the Add Service Reference dialog box to omit choices that are not available in the targeted version.

  • 它會在有多個控制項可供使用時,篩選 [工具箱] 中的自訂控制項,以移除目標版本中未提供的控制項,只顯示最新版控制項。It filters custom controls in the Toolbox to remove those that are not available in the targeted version and to show the only the most up-to-date controls when multiple controls are available.

  • 它會篩選 IntelliSense 以省略目標版本中未提供的語言功能。It filters IntelliSense to omit language features that aren't available in the targeted version.

  • 它會篩選 [屬性] 視窗中的屬性,以省略目標版本中未提供的屬性。It filters properties in the Properties window to omit those that aren't available in the targeted version.

  • 它會篩選功能表選項,以省略目標版本中未提供的選項。It filters menu options to omit options that aren't available in the targeted version.

  • 對於組建,它會使用適用於目標版本的編譯器版本和編譯器選項。For builds, it uses the version of the compiler and the compiler options that are appropriate for the targeted version.

注意

  • Framework 目標不保證您的應用程式將會正確執行。Framework targeting does not guarantee that your application will run correctly. 您必須測試應用程式,確定它能以目標版本執行。You must test your application to make sure it runs against the targeted version.
  • 您不能以低於 .NET Framework 2.0 的 Framework 版本為目標。You cannot target framework versions below .NET Framework 2.0.

選取目標 Framework 版本Select a target framework version

當您建立 .NET Framework 專案時,您可以在選取專案範本後選取目標 .NET Framework 版本。When you create a .NET Framework project, you can select the target .NET Framework version after you select a project template. 可用架構的清單包含適用於所選取範本類型的已安裝之架構版本。The list of available frameworks includes the installed framework versions that are applicable to the selected template type. 針對非 .NET Framework 的專案範本 (例如 .NET Core 範本),系統會隱藏 [Framework] 下拉式清單。For non-.NET Framework project templates, for example .NET Core templates, the Framework drop-down list doesn't appear.

VS 2017 的 Framework 下拉式清單

VS 2019 的 Framework 下拉式清單

變更目標 FrameworkChange the target framework

在現有 Visual Basic、C# 或 F# 專案中,您可以在 [專案屬性] 對話方塊中變更目標 .NET 版本。In an existing Visual Basic, C#, or F# project, you change the target .NET version in the project properties dialog box. 如需如何變更 C++ 專案之目標版本的資訊,請改為參閱如何修改目標 Framework 和平台工具組For information about how to change the target version for C++ projects, see How to modify the target framework and platform toolset instead.

  1. 在 [方案總管] 中,開啟您所要變更專案的右鍵功能表,然後選擇 [屬性]。In Solution Explorer, open the right-click menu for the project that you want to change, and then choose Properties.

  2. 在 [ 屬性 ] 視窗的左欄中,選擇 [ 應用程式 ] 索引標籤。In the left column of the Properties window, choose the Application tab.

    [專案屬性應用程式] 索引標籤

    注意

    在建立 UWP 應用程式之後,就無法變更 Windows 或 .NET 的目標版本。After you create a UWP app, you can't change the targeted version of either Windows or .NET.

  3. 在 [目標 Framework] 清單中,選擇您要的版本。In the Target Framework list, choose the version that you want.

  4. 在出現的驗證對話方塊中,選擇 [是] 按鈕。In the verification dialog box that appears, choose the Yes button.

    專案將會卸載。The project unloads. 當您重新載入專案時,它會以您剛剛選擇的 .NET 版本為目標。When it reloads, it targets the .NET version that you just chose.

注意

如果您的程式碼包含與您目標版本不同的 .NET 版本參考,則在您編譯或執行程式碼時,可能會出現錯誤訊息。If your code contains references to a different version of the .NET than the one that you targeted, error messages may appear when you compile or run the code. 若要解決這些錯誤,請修改參考。To resolve these errors, modify the references. 請參閱針對 .NET 目標錯誤進行疑難排解See Troubleshoot .NET targeting errors.

提示

視目標 Framework 而定,它可以在專案檔中以下列方式呈現:Depending on the target framework, it can be represented in the following ways in the project file:

  • 針對 .NET Core 應用程式:<TargetFramework>netcoreapp2.1</TargetFramework>For a .NET Core app: <TargetFramework>netcoreapp2.1</TargetFramework>
  • 針對 .NET Standard 應用程式:<TargetFramework>netstandard2.0</TargetFramework>For a .NET Standard app: <TargetFramework>netstandard2.0</TargetFramework>
  • 針對 .NET Framework 應用程式:<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>For a .NET Framework app: <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>

解析系統與使用者組件參考Resolve system and user assembly references

若要設定目標 .NET 版本,您必須先安裝適當的組件參考。To target a .NET version, you must first install the appropriate assembly references. 您可以在 .NET 下載 (英文) 頁面上下載不同 .NET 版本的開發人員套件。You can download developer packs for different versions of .NET on the .NET downloads page.

針對 .NET Framework 專案,[新增參考] 對話方塊會停用與目標 .NET Framework 版本無關的系統組件,如此一來就不會不慎將那些系統組件新增至專案。For .NET Framework projects, the Add Reference dialog box disables system assemblies that do not pertain to the target .NET Framework version so that they cannot be inadvertently added to a project. (系統元件是包含在 .NET Framework 版本中的 .dll 檔。不會解析屬於高於目標版本之 Framework 版本的 ) 參考,也無法新增相依于這類參考的控制項。(System assemblies are .dll files that are included in a .NET Framework version.) References that belong to a framework version that's higher than the targeted version will not resolve, and controls that depend on such a reference cannot be added. 如果您想要啟用這類參考,請將專案的 .NET Framework 目標重設為包含參考的目標。If you want to enable such a reference, reset the .NET Framework target of the project to one that includes the reference.

如需組件參考的詳細資訊,請參閱在設計階段時解析組件For more information about assembly references, see Resolve assemblies at design time.

啟用 LINQEnable LINQ

當您以 .NET Framework 3.5 或更新版本為目標時,會自動新增 System.Core 的參考與 System.Linq 的專案層級匯入 (僅限 Visual Basic)。When you target the .NET Framework 3.5 or later, a reference to System.Core and a project-level import for System.Linq (in Visual Basic only) are added automatically. 如果要使用 LINQ 功能,您必須同時開啟 Option Infer (僅限 Visual Basic)。If you want to use LINQ features, you must also turn Option Infer on (in Visual Basic only). 如果將目標變更為舊版 .NET Framework,就會自動移除參考和匯入。The reference and import are removed automatically if you change the target to an earlier .NET Framework version. 如需詳細資訊,請參閱使用 LINQFor more information, see Work with LINQ.

另請參閱See also