Visual Studio 2019 project upgrade to C#8 build failed

一笑奈何 提交于 2020-08-09 10:46:04

问题


For one of our projects we're trying to upgrade to use C#8 instead of C#7.1. The upgrade looks like it works, because the compiler looks like it takes C#8 statements (it doesn't mark it as an error). However, when I build the project I get a build failed without any errors showing initially.

The code to test C#8 is of the following lines:

private string TestCSharp8(int x)
{
    return x switch
    {
        0 => "ZERO",
        1 => "ONE",
        2 => "TWO",
        3 => "THREE",
        _ => "OTHER"
    };
}

The IDE is accepting this code and even suggests to reformat it to this when writing an 'old-fashioned' switch statement. However, when I then build the solution I get the following output in the Error List:

But then the Ouput log shows the following:

When I look at the build log after I put the Ouput verbosity to detailed, I can see the following errors occur:

1>C:\VisualStudio\Repos\XXX\YYY\ZZZ\Repositories\ABC.cs(301,5,301,6): error CS1597: Semicolon after method or accessor block is not valid
1>C:\VisualStudio\Repos\XXX\YYY\ZZZ\Repositories\ABC.cs(304,1,304,2): error CS1022: Type or namespace definition, or end-of-file expected
1>Done executing task "Csc" -- FAILED.
1>Done building target "CoreCompile" in project "XYZ.csproj" -- FAILED.

In the project file I have set the <LangVersion> to latest. The target framework we're targetting is .NET Framework 4.7.1.

The strange thing is all of this works and builds in another solution we're having.

So, can someone please help me in the right direction on how to fix this? At the moment I don't know where to look for the solution.


回答1:


From the docs:

C# 8.0 is supported on .NET Core 3.x and .NET Standard 2.1.

And:

C# 8.0 (and higher) is supported only on .NET Core 3.x and newer versions. Many of the newest features require library and runtime features introduced in .NET Core 3.x.




回答2:


I found the answer which helped me solve this. In my case I had to update the Microsoft.Net.Compilers package to the latest version as well. Now it builds without any errors.



来源:https://stackoverflow.com/questions/60828216/visual-studio-2019-project-upgrade-to-c8-build-failed

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