如何在 Visual Studio 2015 中更改 VB.NET 语言版本

如何在 Visual Studio 2015 中更改 VB.NET 语言版本

问题描述:

在 Visual Studio 2015 中,可以选择正在编码的 C# 语言版本,如图所示 此处.

In Visual Studio 2015 it is possible to select which version of the C# language is being coded in, as shown here.

我正在为 VB.NET 寻找相同的选项 - 如何将语法等限制为旧的 VB.NET 版本?

I'm looking for the same option for VB.NET - how can I restrict syntax, etc. to old VB.NET versions?

我想这样做是为了避免在我与某人使用 Visual Studio 2012.我宁愿不使用 Visual Studio 2012 安装向我的机器发送垃圾邮件,也不想为相当偶然的需求创建一个新的 VM.

I want to do this so that I stop accidentally using VB 14 features in a project I'm sharing with someone using Visual Studio 2012. I'd rather not spam up my machine with a Visual Studio 2012 install or have to create a new VM for a fairly occasional requirement.

注意:我不想更改目标 .NET Framework 版本.

NOTE: I don't want to change the target .NET Framework version.

VB.NET 编译器具有 /langversion 选项.MSBuild 也支持.但不是通过 IDE,这使得更改它很尴尬.

The VB.NET compiler has the /langversion option for this. Also supported by MSBuild. But not by the IDE, that makes it awkward to change it.

仍有可能,您必须编辑 .vbproj 文件.使用文本编辑器,记事本就行.并复制/粘贴此代码段,将其插入第 4 行,使其对所有配置和平台均有效:

Still possible, you have to edit the .vbproj file. Use a text editor, Notepad will do. And copy/paste this snippet, insert it in the 4th line so it is effective for all configurations and platforms:

  <PropertyGroup>
     <LangVersion>12</LangVersion>
  </PropertyGroup>

并仔细检查它是否有效:

And double-check that it is effective:

Module Module1
    Sub Main()
        Dim test As String
        Console.WriteLine(NameOf(test))
    End Sub
End Module

输出:

error BC36716: Visual Basic 12.0 does not support 'nameof' expressions.

嗯,这行得通,IntelliSense 也用红色波浪线标记.您可能想要创建自己的项目模板,这样您就不必一遍又一遍地这样做.使用文件 > 导出模板.

Well, that works, also flagged by IntelliSense with red squiggles. You probably want to create your own project templates so you don't have to do this over and over again. Use File > Export Template.