How can .csproj know about its solution?

巧了我就是萌 提交于 2019-12-07 05:32:13

问题


In .csproj files you can use variables like $(SolutionDir). It's possible to find libraries that way.

How it's possible you can build .csproj by msbuild (not giving it solution path)? I can't see any sense in such behaviour, but it's working anyway.


回答1:


Individual project files do not have awareness of what solution they are part of. Variables $(SolutionDir), $(SolutionPath), $(SolutionExt), etc, only defined when you build your .sln file, they are not defined when you build project directly.

To illustrate this, place this in your project file:

<Target Name="BeforeBuild">
  <Message Text="SolutionDir=$(SolutionDir)" />
</Target>

When you build the solution you get this:

>msbuild sln.sln
Microsoft (R) Build Engine version 12.0.31101.0
...
BeforeBuild:
  SolutionDir=e:\temp\sln\
...

When you build your project directly you get this:

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

As you can see, $(SolutionDir) is badly broken. I don't recommend using it, along with other $(Solution***) variables, because you will want at some point to build project directly, or create new .sln file with different subset of your projects in other directory, and then you will run into build breaks.



来源:https://stackoverflow.com/questions/28196339/how-can-csproj-know-about-its-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!