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.

重要

尽管名称为“多定向”,一个项目一次只能针对一个框架和一个平台。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.

  • 可以一个 框架配置文件 为目标,该文件是目标框架的预定义子集。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 多定向功能可保证应用程序仅使用目标框架和平台中的可用功能。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 应用程序设定为在与 80x86 处理器系列 (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 80x86 processor family (x86). 目标框架与目标平台的组合称为“目标上下文”。The combination of target framework and target platform is known as the target context. 有关详细信息,请参阅目标框架和目标平台For more information, see Target framework and target platform.

工具集 (ToolsVersion)Toolset (ToolsVersion)

工具集可收集用于创建应用程序的工具、任务和目标。A Toolset collects together the tools, tasks, and targets that are used to create the application. 工具集包括编译器(如 csc.exe 和 vbc.exe)、常见目标文件 (microsoft.common.targets) 及常见任务文件 (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. 有关详细信息,请参阅工具集 (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.