如何更改所有项目的Visual Studio 2017默认语言版本? [英] How to change Visual Studio 2017 default language version for all projects?

查看:586
本文介绍了如何更改所有项目的Visual Studio 2017默认语言版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#7.0上为所有项目设置语言版本?

How to set language version on C#7.0 for all projects?

默认参数来自哪里,我想更改默认值

Where does default parameter come from, I want to change default value

PS:我不是说UI语言

PS: I do not mean the UI language

推荐答案

default值的含义来自C#编译器本身.因此,要进行更改,就意味着您需要更改编译器.

The meaning of default value comes from the C# compiler itself. So in order to change it's meaning you need to change the compiler.

但是在Visual Studio 2017随附的编译器中,default实际上表示C#7.0,因此您无需执行任何操作.

But in compiler that comes with Visual Studio 2017 default actually means C# 7.0, so you don't need to do anything.

Visual Studio Project System仅将语言版本值传递给MSBuild. MSBuild将它作为/langversion选项进一步传递给C#编译器. /langversion选项使您可以指定编译器接受的较高语言版本.换句话说,它允许您将语言功能的使用限制为特定版本.如果您使用的语言版本比指定的语言版本高,则C#编译器将发出错误消息.就这样.如果将/langversion指定为default,则C#编译器将接受最后一个主要C#语言版本包括的所有有效语法(请参见以C#语言添加的功能C#GitHub存储库上的版本页面.

Visual Studio Project System merely passes language version value to MSBuild. And MSBuild passes it further to C# compiler as a /langversion option. /langversion option lets you specify an upper language version that compiler accepts. In another words it allows you to restrict language features usage to a certain version. If you use feature from language version higher than you specified, C# compiler will emit error. That's all. If you specify /langversion as default, C# compiler will accept all valid syntax that last major C# language version include (see /langversion (C# Compiler Options) page on MSDN). Last major C# version that comes with Visual Studio 2017 is 7.0. See Features Added in C# Language Versions page on C# GitHub repository.

如果需要启用最新的次要版本(7.1、7.2等)的功能或一次禁止对多个项目或解决方案使用某些新的C#功能,则可以使用MSBuild 15 Directory.Build.props自定义文件.有关MSDN上自定义构建文章的相关摘录:

If you need to enable features of latest minor versions (7.1, 7.2 etc.) or forbid usage of some new C# features for multiple projects or solutions at once you can use MSBuild 15 Directory.Build.props customization file. Relevant excerpt from Customize your build article on MSDN:

...现在,您可以在仓库的根目录下的一个名为Directory.Build.props的文件中对其进行定义,从而一步一步将新属性添加到每个项目中.运行MSBuild时,Microsoft.Common.props在目录结构中搜索Directory.Build.props文件(Microsoft.Common.targets查找Directory.Build.targets).如果找到一个,则导入该属性. Directory.Build.props是用户定义的文件,可为目录下的项目提供自定义.

...now you can add a new property to every project in one step by defining it in a single file called Directory.Build.props at the root of your repo. When MSBuild runs, Microsoft.Common.props searches your directory structure for the Directory.Build.props file (and Microsoft.Common.targets looks for Directory.Build.targets). If it finds one, it imports the property. Directory.Build.props is a user-defined file that provides customizations to projects under a directory.

以下Directory.Build.props文件的示例指示C#编译器在所有项目中接受最新的次要C#版本(Visual Studio 2017版本15.5.3中的C#7.2)的所有有效语法,因为它们的.csproj文件不存在包含优先级为<LangVersion>的标签:

Following example of a Directory.Build.props file instructs C# compiler to accept all valid syntax of a latest minor C# version (C# 7.2 in Visual Studio 2017 version 15.5.3) in all projects given their .csproj file doesn't include <LangVersion> tag which takes precedence:

<Project>
    <PropertyGroup>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>
</Project>

有关更多信息,请检查:

For more information check:

  • What does the langversion switch do? article by Eric Lippert
  • Project System source code
  • Common MSBuild project properties page on MSDN, CscToolPath property in particular

这篇关于如何更改所有项目的Visual Studio 2017默认语言版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆