我正在使用 Visual Studio 2019,我正在尝试更改我的 C# 版本。我这样做的原因是我使用的构建服务器使用旧版本的 VS / MSBuild 来构建和部署代码(这是我无法控制的)。因此我需要使用 C# 5。
在 Visual Studio 的早期版本中,您可以通过 [Project] -> Properties -> Build -> Advanced 中的菜单执行此操作。
对于 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>
...
知道我怎样才能做到这一点吗?这可能是我错过的一些非常愚蠢的事情。
注:我确实看到了this https://stackoverflow.com/questions/56149841/visual-studio-2019-version-c-sharp-probleme上一个问题,但缺乏细节。
事实证明@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>
SOLUTION:
我删除了所有这些配置特殊情况,并且它默认返回到我在文件顶部列出的语言。
我可以说它在我的情况下有效,因为它现在拒绝插值字符串(例如$"{value}words"
)说它在 C#5 中不可用。
至于什么语言名称有效,“5”和“5.0”都适合我。其余选项可以找到here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#c-language-version-reference
我现在感觉有点愚蠢,我希望这个问题对未来的人仍然有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)