.NET Core项目编译时如何确定需包含的文件?兼谈与.NET Standard差异
Great observation! This difference comes down to the shift from traditional project files to SDK-style project files in .NET Core (and modern .NET versions overall), which embrace the principle of convention over configuration to simplify project management.
The .NET SDK removes the need for manual file listings by using sensible default rules. Here's the breakdown:
Default Inclusion Rules
Instead of declaring every file explicitly, the SDK automatically includes files based on these conventions:
- All code files (
.cs,.vb,.fs, etc.) in the project root and all subdirectories are added to the compilation by default. - Related paired files (like
.xamland their.xaml.cscode-behind files) are automatically linked—no need for<DependentUpon>entries anymore; the SDK handles this association. - Common assets like resource files (
.resx), configuration files (e.g.,appsettings.json), and static web files follow type-specific inclusion rules based on their location.
Overriding the Defaults
If you need to adjust the file set, you can modify the .csproj directly:
- Exclude files/directories: Use the
Removeattribute in an item group to exclude specific files or patterns. For example:<ItemGroup> <!-- Exclude all files in the "Temp" directory and its subdirectories --> <Compile Remove="Temp\**" /> <!-- Exclude a single unused code file --> <Compile Remove="LegacyCode.cs" /> </ItemGroup> - Manually include files: If you have files outside the project directory or need to add non-standard files, use the
Includeattribute:<ItemGroup> <!-- Include shared code from a parent directory --> <Compile Include="..\SharedUtils\*.cs" /> </ItemGroup>
Why This Beats Traditional .NET Projects
Older .NET Framework projects required explicit file declarations because they lacked the SDK's convention-based logic. This meant every file addition/removal forced a .csproj edit—leading to bloated project files and frequent merge conflicts in team work. The SDK-style approach eliminates this overhead by letting the framework handle routine file management.
内容的提问来源于stack exchange,提问作者it's me

