首页 > 解决方案 > 在 Visual Studio 2019 中更改 C# 版本

问题描述

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

在以前版本的 Visual Studio 中,您可以从 [项目] -> 属性 -> 构建 -> 高级中的菜单执行此操作。对于 VS2019,微软以其无限的智慧,决定让这件事变得更难。显然您需要手动编辑项目文件并添加:

<PropertyGroup>
   <LangVersion>[some version here]</LangVersion>
</PropertyGroup>

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

<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>
...

知道我怎样才能完成这项工作吗?这可能是我想念的非常愚蠢的事情。注意:我确实看到了这个先前的问题,但它缺乏细节。

标签: c#visual-studio-2019

解决方案


原来@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>

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

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

至于哪些语言名称有效,“5”和“5.0”都对我有用。其余选项可在此处找到

I feel kind of dumb now, I hope this question will still be useful to some future people.


推荐阅读