我正在使用 Visual Studio 2019,并且正在尝试更改我的 C# 版本。我这样做的原因是我使用的构建服务器使用旧版本的 VS / MSBuild 来构建和部署代码(这不在我的控制范围内)。因此我需要使用 C# 5。
在以前版本的 Visual Studio 中,您可以从 [Project] -> Properties -> Build -> Advanced. For VS2019 Microsoft, in their infinite wisdom, has decided to make this harder. Apparently you need to edit the project file manually and add:
<PropertyGroup>
<LangVersion>[some version here]</LangVersion>
</PropertyGroup>
To your project file manually. That's all well and good, but I can't seem to get that working. It just ignores it, even after I unload and reload it. Here is a snapshot of my project file:
<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>
...
Any idea how I can make this work? It's probably something really silly I'm missing. Note: I did see this 上一个问题的菜单中执行此操作,但缺少详细信息。
在Visual Studio 2019中更改C#语言版本的最简单方法是:
1.右键单击Visual Studio中的解决方案资源管理器(文件树)中的项目;
2.然后从弹出菜单中选择“编辑项目项目属性” - >“ C#语言级别”;
3.在列表中,您将看到所有可能的C#版本。
而已!它像魅力一样工作。请注意,您不应该对项目文件进行任何其他更改 - 因此,如果您这样做,请将其恢复为原始状态。另外,您不需要任何标签。