简体   繁体   English

如何在Visual Studio 2015中禁用C#6支持?

[英]How do I disable C# 6 Support in Visual Studio 2015?

Background 背景

We have a project that we're developing in VS 2015 with C#6 enabled that occasionally needs to be opened by developers using VS 2013 without C#6. 我们在VS 2015中开发了一个项目,启用了C#6,偶尔需要由使用VS 2013而没有C#6的开发人员打开。

We have no intention to use C# 6 within this particular solution (as much as I'd like to). 我们无意在这个特定的解决方案中使用C#6(尽我所能)。

Problem 问题

Visual Studio and ReSharper suggest helpful C# 6 language constructs that render the solution inoperable in earlier versions of Visual Studio without C#6 support. Visual Studio和ReSharper建议使用有用的C#6语言结构,这些结构使得解决方案在没有C#6支持的早期版本的Visual Studio中无法运行。

I've disabled the ReSharper C#6 support but I can't seem to disable / limit C# features across the whole solution. 我已经禁用了ReSharper C#6支持,但我似乎无法在整个解决方案中禁用/限制C#功能。

Question

How do I limit C# to C#5 capabilities within a solution or within Visual Studio 2015? 如何在解决方案或Visual Studio 2015中限制C#到C#5的功能?

You can set the language feature for each project separately by going to Properties => Build tab => Advanced button => Language Version and set your preferred version. 您可以分别为每个项目设置语言功能,方法是转到Properties => Build tab => Advanced button => Language Version并设置首选版本。

You should realize that it will still use the new "C# 6.0" .Net Compiler Platform (codenamed Roslyn). 您应该意识到它仍将使用新的“C#6.0”.Net编译器平台(代号为Roslyn)。 However, that compiler will imitate the behavior of older compilers and will limit you to features only available on that specific language version. 但是,该编译器将模仿旧编译器的行为,并将限制您仅在该特定语言版本上可用的功能。


I don't think that there's a solution-wide setting available. 我认为没有可用的解决方案范围设置。

add below in .sln.DotSettings should disable it on solution level .sln.DotSettings下面添加应该在解决方案级别禁用它

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

Or if you don't have a .sln.DotSettings file: 或者,如果您没有.sln.DotSettings文件:

  1. If your solution file is called Apple.sln, create a file beside it called Apple.sln.DotSettings. 如果您的解决方案文件名为Apple.sln,请在其旁边创建一个名为Apple.sln.DotSettings的文件。

  2. Give it the following contents: 给它以下内容:

     <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. 关闭并重新打开解决方案,Resharper应该只警告你关于C#5的事情。

  4. Don't forget to remove this when you eventually start using C#6 features! 当您最终开始使用C#6功能时,不要忘记删除它! :) :)

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

You can set the language feature for all the solutions/csproj with the MSBuildUserExtensionsPath . 您可以使用MSBuildUserExtensionsPath为所有solutions/csproj设置语言功能。

Search the value of the $(MSBuildUserExtensionsPath) , it should be something like C:\\Users\\$(User)\\AppData\\Local\\Microsoft\\MSBuild 搜索$(MSBuildUserExtensionsPath)的值,它应该类似于C:\\Users\\$(User)\\AppData\\Local\\Microsoft\\MSBuild

Then edit the file Force.LangVersion.ImportBefore.props in the folder $(MSBuildUserExtensionsPath)\\14.0\\Imports\\Microsoft.Common.Props\\ImportBefore with : 然后在$(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>

Steps have already been written above, just adding a screenshot further of my VS2015: 上面已经写了一些步骤,只是添加了我的VS2015的截图:

Properties of project >> Build >> Advanced >> Language version 项目属性>> Build >> Advanced >>语言版本

在此输入图像描述

I set that to C# 5.0. 我把它设置为C#5.0。

Right click on Project in Project Explorer and select Properties. 右键单击Project Explorer中的Project,然后选择Properties。

When the Properties tab opens select Build and the click the Advance button in bottom right. 打开“属性”选项卡后,选择“生成”,然后单击右下角的“高级”按钮。

There is drop-down box called Language Version. 有一个名为语言版本的下拉框。 Change the select to "C# 5.0" 将选择更改为“C#5.0”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM