简体   繁体   中英

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

In the old format files were explicitly included in the <Compile...> element. Most files in the new format are not included here, only.cs-files on a sibling level of the.csproj are explicitly included. Typically "Program.cs", et.c.

There are no clear explanations for this. Most just mention "new convention" and "standard include and exclude paths". What are these paths? Does it recursively search child folders under the.csproj folder? If I create another.cs-file with a conflicting type to another one that is "included", could that get mistakenly parsed even if it's not explicitly included in my solution?

The default compilation includes are 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) .

So yes, it recursively searches for .cs files in the project folder. If it finds two files which define the same type, it will try to compile both of them, and you will get compiler errors.

To exclude a file use

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

in an <ItemGroup> . You can also right-click the file in Visual Studio and select "Exclude from Project".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM