在 MSBuild 项目文件中,Private 设置对 ProjectReference 起什么作用?

前几天我在一个项目文件里看到了这个:

<ProjectReference Include="Foo\Bar\Baz.csproj">
<Project>{A GUID HERE}</Project>
<Name>Baz</Name>
<Private>False</Private> <!-- ??? -->
<ReferenceOutputAssembly>False</ReferenceOutputAssembly>
</ProjectReference>

ProjectReference中的每个节点似乎都是不言而喻的(被引用的项目文件,GUID,在解决方案浏览器中显示的名称,以及当前项目是否应该链接到被引用的项目) ,除了 Private,而且 公共 MSBuild 项目项页面没有记录这个值。(有一个针对 Reference而不是 ProjectReferencePrivate设置文档——但它有 NeverAlwaysPreserveNewest设置,不是 true 和 false)

这个设置是做什么的?

71497 次浏览

Private标记维护 VisualStudioReferences 文件夹中“ CopyLocal”复选框的用户覆盖。这控制引用是从 GAC 使用,还是将引用的程序集复制到生成目录。

虽然我找不到任何 MSDN 文档证明这一点(出乎意料) ,但是从行为和 来自 Microsoft.Common.CurrentVersion.targets:1742的评论中可以明显看出它的应用:

这在 MSDN > 公共 MSBuild 项目项中有记载,从行为和 来自 Microsoft.Common.CurrentVersion.targets:1742的评论中可以明显看出:

  <!--
============================================================


ResolveAssemblyReferences


Given the list of assemblies, find the closure of all assemblies that they depend on. These are
what we need to copy to the output directory.


[IN]
@(Reference) - List of assembly references as fusion names.
@(_ResolvedProjectReferencePaths) - List of project references produced by projects that this project depends on.


The 'Private' attribute on the reference corresponds to the Copy Local flag in IDE.
The 'Private' flag can have three possible values:
- 'True' means the reference should be Copied Local
- 'False' means the reference should not be Copied Local
- [Missing] means this task will decide whether to treat this reference as CopyLocal or not.


[OUT]
@(ReferencePath) - Paths to resolved primary files.
@(ReferenceDependencyPaths) - Paths to resolved dependency files.
@(_ReferenceRelatedPaths) - Paths to .xmls and .pdbs.
@(ReferenceSatellitePaths) - Paths to satellites.
@(_ReferenceSerializationAssemblyPaths) - Paths to XML serialization assemblies created by sgen.
@(_ReferenceScatterPaths) - Paths to scatter files.
@(ReferenceCopyLocalPaths) - Paths to files that should be copied to the local directory.
============================================================
-->

我只是想说,<Private>false</Private>(你可以应用到 ProjectReference)可能不工作时,使用 <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Publish" Properties="$(_MSBuildProperties)" />和项目 $(MSBuildProjectFullPath)ProjectReference<None><CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory></None> .我已经阅读了 https://github.com/dotnet/sdk/blob/master/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets的源代码并找到了解决方案。您需要定义 _GetChildProjectCopyToPublishDirectoryItems=false,因此示例应该是: <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="Publish" Properties="TargetFramework=$(TargetFramework);_GetChildProjectCopyToPublishDirectoryItems=false" />