组织项目以支持 .NET Framework 和 .NET CoreOrganize your project to support both .NET Framework and .NET Core

了解如何创建一个并行编译 .NET Framework 和 .NET Core 的解决方案。Learn how to create a solution that compiles for both .NET Framework and .NET Core side-by-side. 查看几个选项来组织项目以帮助你实现此目标。See several options to organize projects to help you achieve this goal. 以下是决定如何使用 .NET Core 设置项目布局时要考虑的一些典型方案。Here are some typical scenarios to consider when you're deciding how to setup your project layout with .NET Core. 此列表可能无法涵盖所有要求;这些方案的优先级具体取决于项目需求。The list may not cover everything you want; prioritize based on your project's needs.

  • 将现有项目和 .NET Core 项目合并为单个项目 Combine existing projects and .NET Core projects into single projects

    此方案的好处:What this is good for:

    • 通过编译单个项目(而非多个项目)简化生成过程,每个项目针对不同的 .NET Framework 版本或平台。Simplifying your build process by compiling a single project rather than compiling multiple projects, each targeting a different .NET Framework version or platform.
    • 由于需要管理单个项目文件,因此简化了多目标项目的源文件管理。Simplifying source file management for multi-targeted projects because you must manage a single project file. 添加/删除源文件时,需要手动将备用文件与其他项目进行同步。When adding/removing source files, the alternatives require you to manually sync these with your other projects.
    • 轻松生成可供使用的 NuGet 包。Easily generating a NuGet package for consumption.
    • 允许通过使用编译器指令为库中特定的 .NET Framework 版本编写代码。Allows you to write code for a specific .NET Framework version in your libraries through the use of compiler directives.

    不支持的方案:Unsupported scenarios:

  • 将现有项目和新的 .NET Core 项目分离 Keep existing projects and new .NET Core projects separate

    此方案的好处:What this is good for:

    • 支持没有安装 Visual Studio 2017 或更高版本的开发人员和参与者基于现有项目开发。Supporting development on existing projects for developers and contributors who may not have Visual Studio 2017 or a later version.
    • 减少现有项目中出现新 bug 的可能性,因为这些项目中不需要进行任何代码改动。Decreasing the possibility of creating new bugs in existing projects because no code churn is required in those projects.

示例Example

请考虑以下存储库:Consider the repository below:

现有项目

源代码 Source Code

根据现有项目的约束和复杂性,有几种不同的方法可为此存储库添加对 .NET Core 的支持,下面描述了这些方法。The following describes several ways to add support for .NET Core for this repository depending on the constraints and complexity of the existing projects.

将现有项目替换为多目标的 .NET Core 项目Replace existing projects with a multi-targeted .NET Core project

重新组织存储库,以便删除任何现有的 *.csproj 文件,并创建以多个框架为目标的单一 *.csproj 文件。Reorganize the repository so that any existing *.csproj files are removed and a single *.csproj file is created that targets multiple frameworks. 这是一项不错的选择,因为单个项目可以编译不同的框架。This is a great option because a single project is able to compile for different frameworks. 它还可以处理每个目标框架的不同编译选项和依赖项。It also has the power to handle different compilation options and dependencies per targeted framework.

创建以多个框架为目标的 csproj

源代码 Source Code

需注意的更改:Changes to note are:

  • 用新的 .NET Core *.csproj 替换 packages.config 和 *.csproj 。Replacement of packages.config and *.csproj with a new .NET Core *.csproj. NuGet 包是使用 <PackageReference> ItemGroup 指定的。NuGet packages are specified with <PackageReference> ItemGroup.

保留现有项目并创建 .NET Core 项目Keep existing projects and create a .NET Core project

如果存在以较旧框架为目标的现有项目,可能需要保留这些项目并将 .NET Core 项目用作将来框架的目标。If there are existing projects that target older frameworks, you may want to leave these projects untouched and use a .NET Core project to target future frameworks.

位于不同文件夹中的现有项目与 .NET Core 项目

源代码 Source Code

需注意的更改:Changes to note are:

  • 将 .NET Core 项目和现有项目保存在不同的文件夹中。The .NET Core and existing projects are kept in separate folders.
    • 将项目保存在不同的文件夹中可以避免强制使用 Visual Studio 2017 或更高版本。Keeping projects in separate folders avoids forcing you to have Visual Studio 2017 or later versions. 可以创建仅打开旧项目的单独解决方案。You can create a separate solution that only opens the old projects.

请参阅See also