如何检测到 .cs 文件以包含在新的 .csproj SDK 格式中?

How .cs-files detected for inclusion in the new .csproj SDK format?

在旧格式中,文件明确包含在 元素中。大多数新格式的文件未包含在此处,仅明确包含 .csproj 同级级别的 .cs 文件。通常为“Program.cs”、et.c.

对此没有明确的解释。大多数只提到“新约定”和“标准包含和排除路径”。这些路径是什么?它是否递归搜索 .csproj 文件夹下的子文件夹?如果我创建另一个 .cs 文件,其类型与另一个“包含”的文件有冲突,即使它没有明确包含在我的解决方案中,是否会被错误地解析?

默认编译包括documented here:

Default compilation includes

The default includes and excludes for compile items, embedded resources, and None items are defined in the SDK. Unlike non-SDK .NET Framework projects, you don't need to specify these items in your project file, because the defaults cover most common use cases. This makes the project file smaller and easier to understand and edit by hand, if needed.

The following table shows which elements and which globs are included and excluded in the .NET SDK:

Element Include glob Exclude glob > Remove glob
Compile **/*.cs (or other language extensions) **/*.user; **/*.*proj; **/*.sln; **/*.vssscc N/A
EmbeddedResource **/*.resx **/*.user; **/*.*proj; **/*.sln; **/*.vssscc N/A
None **/* **/*.user; **/*.*proj; **/*.sln; **/*.vssscc **/*.cs; **/*.resx

Note The ./bin and ./obj folders, which are represented by the $(BaseOutputPath) and $(BaseIntermediateOutputPath) MSBuild properties, are excluded from the globs by default. Excludes are represented by the property $(DefaultItemExcludes).

所以是的,它在项目文件夹中递归搜索 .cs 文件。如果它找到两个定义相同类型的文件,它会尝试编译它们,你会得到编译器错误。

排除文件使用

<Compile Remove="Path/To/File.cs"/>

<ItemGroup> 中。您也可以右键单击 Visual Studio 中的文件和 select“从项目中排除”。