.csproj如何知道其解决方案?

Mac*_*bły 5 .net c# msbuild csproj

在.csproj文件中,您可以使用$(SolutionDir)之类的变量.可以通过这种方式找到库.

你怎么可能通过msbuild构建.csproj(不给它解决方案路径)?我在这种行为中看不出任何意义,但无论如何它都在起作用.

sev*_*tov 9

单个项目文件无法了解它们所属的解决方案.变量$(SolutionDir),$(SolutionPath),$(SolutionExt)等,仅在构建.sln文件时定义,在直接构建项目时不会定义它们.

为了说明这一点,请将其放在项目文件中:

<Target Name="BeforeBuild">
  <Message Text="SolutionDir=$(SolutionDir)" />
</Target>
Run Code Online (Sandbox Code Playgroud)

构建解决方案时,您会得到:

>msbuild sln.sln
Microsoft (R) Build Engine version 12.0.31101.0
...
BeforeBuild:
  SolutionDir=e:\temp\sln\
...
Run Code Online (Sandbox Code Playgroud)

直接构建项目时,您会得到:

>msbuild fs\fs.fsproj
Microsoft (R) Build Engine version 12.0.31101.0 
...
BeforeBuild:
  SolutionDir=*Undefined*
...
Run Code Online (Sandbox Code Playgroud)

如您所见,$(SolutionDir)严重受损.我不推荐使用它,以及其他$(Solution***)变量,因为您希望在某些时候直接构建项目,或者在其他目录中创建具有项目的不同子集的新.sln文件,然后你会遇到构建中断.