如何在 VisualStudio2015中禁用 C # 6支持?

背景资料

我们有一个项目,我们正在开发的 VS 2015与 C # 6启用,偶尔需要开发人员打开使用 VS 2013没有 C # 6。

我们并不打算在这个特定的解决方案中使用 C # 6(尽管我很想这么做)。

问题

Visual Studio 和 ReSharper 提供了一些有用的 C # 6语言结构,它们使得在没有 C # 6支持的 Visual Studio 早期版本中的解决方案不可操作。

我有 禁用了 ReSharper C # 6支持,但似乎不能在整个解决方案中禁用/限制 C # 特性。

提问

如何在解决方案或 VisualStudio2015中将 C # 限制为 C # 5功能?

27581 次浏览

通过转到 Properties => Build tab => Advanced button => Language Version并设置首选版本,可以分别为每个项目设置语言特性。

您应该意识到,它仍将使用新的“ C # 6.0”。Net Compiler Platform (代号 Roslyn)。但是,该编译器将 模仿的行为较老的编译器,并将限制您的功能,只有在该特定的语言版本。


我不认为有一个解决方案范围的设置可用。

在“项目资源管理器”中右键单击“项目”并选择“属性”。

当“属性”选项卡打开时,选择“生成”,然后单击右下角的“前进”按钮。

有一个名为“语言版本”的下拉框。将选择更改为“ C # 5.0”

.sln.DotSettings中添加下面的代码应该在解决方案级别禁用它

<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp50</s:String>

或者如果你没有 .sln.DotSettings文件:

  1. 如果您的解决方案文件名为 Apple.sln,那么在它旁边创建一个名为 Apple.sln.DotSettings 的文件。

  2. 给它下面的内容:

    <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp50</s:String>
    </wpf:ResourceDictionary>
    
  3. Close and reopen the solution, Resharper should only warn you about C#5 things.

  4. Don't forget to remove this when you eventually start using C#6 features! :)

您可以使用 MSBuildUserExtensionsPath设置所有 solutions/csproj的语言特性。

搜索 $(MSBuildUserExtensionsPath)的值,它应该类似于 C:\Users\$(User)\AppData\Local\Microsoft\MSBuild

然后使用以下命令编辑文件夹 $(MSBuildUserExtensionsPath)\14.0\Imports\Microsoft.Common.Props\ImportBefore中的文件 Force.LangVersion.ImportBefore.props:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LangVersion>5</LangVersion>
</PropertyGroup>
</Project>

如果您有许多需要设置 LangVersion的项目,那么我编写的这个 工具可能会对您有所帮助。

步骤已经写在上面,只是添加了我的 VS2015的进一步截图:

项目属性 > 建立 > > 高级 > > 语言版本

enter image description here

我设置为 C # 5.0。