MSBuild 多目標概觀MSBuild multitargeting overview

您可以使用 MSBuild,將應用程式編譯為在數個 .NET Framework 版本中的任一版上或數個系統平台中的任一個平台上執行。By using MSBuild, you can compile an application to run on any one of several versions of the .NET Framework, and on any one of several system platforms. 例如,您可以將應用程式編譯為在 32 位元平台的 .NET Framework 2.0 上執行,也可以將同一個應用程式編譯為在 64 位元平台的 .NET Framework 4.5 上執行。For example, you can compile an application to run on the .NET Framework 2.0 on a 32-bit platform, and compile the same application to run on the .NET Framework 4.5 on a 64-bit platform.

Important

除了名稱「多目標」之外,專案一次只能以一個架構和一個平台為目標。Despite the name "multitargeting", a project can target only one framework and only one platform at a time.

這些是一部分的 MSBuild 目標功能︰These are some of the features of MSBuild targeting:

  • 您可以開發以較舊版 .NET Framework (例如 2.0、3.5 或 4 版) 為目標的應用程式。You can develop an application that targets an earlier version of the .NET Framework, for example, versions 2.0, 3.5, or 4.

  • 您可以將 .NET Framework 以外的架構作為目標,例如 Silverlight Framework。You can target a framework other than the .NET Framework, for example, the Silverlight Framework.

  • 您可以將「Framework 設定檔」 當做目標,這是預先定義的目標 Framework 子集。You can target a framework profile, which is a predefined subset of a target framework.

  • 如果 .NET Framework 目前版本的 Service Pack 已發行,您可以將它當做目標。If a service pack for the current version of the .NET Framework is released, you could target it.

  • MSBuild 多目標可保證應用程式只使用目標 Framework 和平台中提供的功能。MSBuild targeting guarantees that an application uses only the functionality that is available in the targeted framework and platform.

目標架構和平台Target framework and platform

「目標架構」 是建置專案以在其上執行的 .NET Framework 版本,而「目標平台」 是建置專案以在其上執行的系統平台。A target framework is the version of the .NET Framework that a project is built to run on, and a target platform is the system platform that the project is built to run on. 例如,您可能想要設定 .NET Framework 2.0 應用程式,在與 802x86 處理器系列 (x86) 相容的 32 位元平台上執行。For example, you might want to target a .NET Framework 2.0 application to run on a 32-bit platform that is compatible with the 802x86 processor family (x86). 目標 Framework 和目標平台的組合稱為「目標內容」 。The combination of target framework and target platform is known as the target context. 如需詳細資訊,請參閱目標架構和目標平台For more information, see Target framework and target platform.

Toolset (ToolsVersion)Toolset (ToolsVersion)

工具組會將工具、工作以及用來建立應用程式的目標收集在一起。A Toolset collects together the tools, tasks, and targets that are used to create the application. 工具組包括 csc.exevbc.exe 這類編譯器、一般 targets 檔案 (microsoft.common.targets) 和一般 tasks 檔案 (microsoft.common.tasks)。A Toolset includes compilers such as csc.exe and vbc.exe, the common targets file (microsoft.common.targets), and the common tasks file (microsoft.common.tasks). 4.5 工具組可以用來以 .NET Framework 版本 2.0、3.0、3.5、4 和 4.5 為目標。The 4.5 Toolset can be used to target .NET Framework versions 2.0, 3.0, 3.5, 4, and 4.5. 不過,2.0 工具組只能用來以 .NET Framework 版本 2.0 為目標。However, the 2.0 Toolset can only be used to target the .NET Framework version 2.0. 如需詳細資訊,請參閱 Toolset (ToolsVersion)For more information, see Toolset (ToolsVersion).

參考組件Reference assemblies

工具組中所指定的參考組件可協助您設計和建置應用程式。The reference assemblies that are specified in the Toolset help you design and build an application. 這些參考組件不僅會啟用特定目標組建,也會將 Visual Studio IDE 中的元件和功能限制為與目標相容的元件和功能。These reference assemblies not only enable a particular target build, but also restrict components and features in the Visual Studio IDE to those that are compatible with the target. 如需詳細資訊,請參閱在設計階段解析組件For more information, see Resolve assemblies at design time.

設定目標和工作Configure targets and tasks

您可以設定 MSBuild 目標和工作以跨處理序方式隨 MSBuild 一起執行,如此您就能以視為與目前執行之內容不同的內容作為目標。You can configure MSBuild targets and tasks to run out-of-process with MSBuild so that you can target contexts that are considerably different than the one you are running on. 例如,您可以在開發電腦以具有 .NET Framework 4.5 的 64 位元平台執行時,以 32 位元 .NET Framework 2.0 應用程式為目標。For example, you can target a 32-bit, .NET Framework 2.0 application while the development computer is running on a 64-bit platform with .NET Framework 4.5. 如需詳細資訊,請參閱設定目標和工作For more information, see Configure targets and tasks.

疑難排解Troubleshooting

如果您嘗試參考不屬於目標內容的組件,則可能會發生錯誤。You might encounter errors if you try to reference an assembly that is not part of the target context. 如需這些錯誤和其處理方式的詳細資訊,請參閱針對 .NET Framework 目標錯誤進行疑難排解For more information about these errors and what to do about them, see Troubleshoot .NET Framework targeting errors.