Using C# 7.1 with MSBuild

后端 未结 6 1168
粉色の甜心
粉色の甜心 2020-12-16 10:44

To use the new C# 7.1 language features with Visual Studio 2017, you add the setting latest to your project file(s).

相关标签:
6条回答
  • 2020-12-16 11:02

    I've got a solution with a C# console app using C# 7.1 here.

    Using the VS 2017 command line (and thus MSBuild 15.3.409.57025) it worked fine. (The .csproj does contain <LangVersion>latest</LangVersion>.)

    Is this feature just not yet supported by MSBuild, or is it possible to get this working?

    Yes it is.

    Which instance of csc.exe is being run and what's its version? Because it looks like, despite quoting the version, you have the wrong version of csc.exe (the error message says 1-6 so not even C# 7 would work).

    0 讨论(0)
  • 2020-12-16 11:03

    Make sure you have changed for "All Configuration" and not just "Debug"

    else you will be baffling why it is failing at production.

    0 讨论(0)
  • 2020-12-16 11:10

    Nuget packages

    • Microsoft.Net.Compilers nuget package does not work and needn't to be installed.

    Set the following project/build settings

    • Set at least C# 7.1 or higher in the Debug and Release build properties. (via: Project menu > [ProjectName] Properties > Build tab > [Advanced] button > Language Version).

    • Setting it to latest does not work.

    Also make sure that you are running the latest MSBuild version.

    0 讨论(0)
  • 2020-12-16 11:16

    Add a reference to the Microsoft.Net.Compilers package (version 2.3.x, to get C# 7.1).

    A short description of the package:

    Microsoft.Net.Compilers

    This package not only includes the C# and Visual Basic compilers, it also modifies MSBuild targets so that the included compiler versions are used rather than any system-installed versions. Once installed, this package requires Microsoft Build Tools 2015.

    0 讨论(0)
  • 2020-12-16 11:24

    In case you land here because you get the error as the OP mentioned, running msbuild via command line (e.g. from a build agent such as jenkins), the solution may be as easy as to upgrade Microsoft Build Tools 2015.

    You can do that via choco install microsoft-build-tools or manually via the official Microsoft Build Tools 2015 or by updating your Visual Studio 2017 installation.

    0 讨论(0)
  • 2020-12-16 11:27

    We discovered that our MVC projects were triggering this issue.

    To fix, we updated the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package used by these projects from version 1.0.0 to 1.0.7.

    0 讨论(0)
提交回复
热议问题