在 Visual Studio 2019 中更改 C# 版本

Arm*_*ard 9 c# visual-studio-2019

我正在使用 Visual Studio 2019,并且正在尝试更改我的 C# 版本。我这样做的原因是我使用的构建服务器使用旧版本的 VS/MSBuild 来构建和部署代码(这是我无法控制的)。因此我需要使用 C# 5。

在早期版本的 Visual Studio 中,您可以从 [Project] -> Properties -> Build -> Advanced 中的菜单执行此操作。对于 VS2019 微软,以其无限的智慧,决定让这更难。显然您需要手动编辑项目文件并添加:

<PropertyGroup>
   <LangVersion>[some version here]</LangVersion>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

手动添加到您的项目文件。这一切都很好,但我似乎无法让它发挥作用。它只是忽略它,即使在我卸载并重新加载它之后。这是我的项目文件的快照:

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
   <LangVersion>5</LangVersion>
  </PropertyGroup>
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{58FE5465-851B-471F-B6A9-9C861FA5C022}</ProjectGuid>
    <OutputType>Library</OutputType>
...
Run Code Online (Sandbox Code Playgroud)

知道我怎样才能做到这一点吗?这可能是我错过的一些非常愚蠢的事情。注意:我确实看到上一个问题,但它缺乏细节。

Arm*_*ard 7

事实证明 @lennart 是对的:文件中还有一些其他<LangVersion>的。VS 已经将它们隐藏在一些构建配置中。

    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\x86\Debug\</OutputPath>
    <DefineConstants>TRACE;DEBUG;PLATFORM_X86</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x86</PlatformTarget>
    <LangVersion>7.3</LangVersion> <-- HERE!!
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
    <OutputPath>bin\x86\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
Run Code Online (Sandbox Code Playgroud)

解决方案:我删除了所有这些配置特殊情况,它默认返回到我在文件顶部列出的语言。

我可以说它在我的情况下有效,因为它现在拒绝内插字符串(例如$"{value}words"),说它在 C#5 中不可用。

至于什么语言名称有效,“5”和“5.0”都对我有用。其余选项可以在这里找到

我现在觉得有点傻,我希望这个问题对未来的人仍然有用。