.netvisual-studiomsbuilddevenvc#-7.2

Compile C# 7.2 project from command line


I am creating test automation for my code generation library. I have a test project in C# 7.2. The project compiles with Visual Studio without any issues. My test is updating the code of this project. Then I want the test to compile the project, load the assembly, and verify it works as expected.

I tried both msbuild and csc. Both are complaining that 7.2 is too high for them.

I guess there should be a way to compile the project with devenv, which is already installed and works perfectly via UI.

Is there?


Solution

  • For C# 7.2 you'll have to use the MSBuild version shipped with VS2017 (Version 15). Assuming that the command line runs on a machine where VS2017 Professional is installed, the correct MSBuild path should be

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe
    

    Any other version of MSBuild should fail to compile C# 7. Specifically, I am building C# 7.2 projects with MSBuild 15.9 and it works.

    Edit

    You could install MSBuild 15 with the build tools for Visual Studio (see here). I don't know fow sure which version will be installed, but I assume that it'd be the latest. Please note that according to this answer the path will be slightly different.