vb.net Visual Studio 2015 RC 中的界面上出现错误“Visual Basic 9.0 不支持自动实现的属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31160603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:15:56  来源:igfitidea点击:

Error "Visual Basic 9.0 does not support auto-implemented properties" on an interface in Visual Studio 2015 RC

asp.netvb.netvisual-studio-2015

提问by Daz

I have opened a website project that has previously been developed in Visual Studio 2012 in 2015 RC. The project targets .net 3.5.

我已经在2015 RC中打开了一个之前在Visual Studio 2012中开发的网站项目。该项目面向 .net 3.5。

I have this interface defined:

我定义了这个接口:

Public Interface ICurrentStep
    Property outerstep() As String
    Property innerstep() As String
End Interface

I get the following build error for each property: "BC36716 Visual Basic 9.0 does not support auto-implemented properties."

我收到每个属性的以下构建错误:“BC36716 Visual Basic 9.0 不支持自动实现的属性。”

I don't understand why Visual Studio 2012 is perfectly happy with this but 2015 is not. The website works fine under .net 3.5 in both xcopy and published versions.

我不明白为什么 Visual Studio 2012 对此非常满意,而 2015 则不然。该网站在 xcopy 和已发布版本的 .net 3.5 下都可以正常工作。

I also don't understand how would I define the interface any other way. Could this be a 2015/Roslyn bug?

我也不明白我将如何以其他方式定义接口。这可能是 2015/Roslyn 的错误吗?

Targeting .net 4.0 does remove the problem but that's not an option for deployment at the moment due to some external dependencies. I presume that's because behind the scenes it's targeting a different compiler as per Is it possible to force Visual Studio 2010 to use Visual Basic 10?

针对 .net 4.0 确实消除了该问题,但由于某些外部依赖关系,目前这不是部署的选项。我认为这是因为它在幕后针对不同的编译器,是否可以强制 Visual Studio 2010 使用 Visual Basic 10?

采纳答案by Damien_The_Unbeliever

This does indeed appear to be a bug in the Roslyn compiler. The compiler is running in an odd mode where its checking (but not really compiling) the code within App_Code- that code actually gets compiled when the site starts up.

这确实似乎是 Roslyn 编译器中的一个错误。编译器以一种奇怪的模式运行,它检查(但不是真正编译)其中的代码App_Code- 该代码实际上是在站点启动时编译的。

Since it knows that you've set the code to run under v3.5, it assumes that the code will actually be compiled by the "v2.0" compiler, so it's effectively running the check/compile as if you've specified the /langversionflag as 9.

由于它知道您已将代码设置为在 v3.5 下运行,因此它假定代码实际上将由“v2.0”编译器编译,因此它可以有效地运行检查/编译,就好像您已指定/langversion标记为 9。

So, that's why the error message is talking about things not supported by Visual Basic 9. However, if you compile the code with real VB9 compiler, it of course compiles fine.

所以,这就是为什么错误消息谈论的是 Visual Basic 9 不支持的东西。但是,如果你用真正的 VB9 编译器编译代码,它当然编译得很好。

As evidence that the compiler is rather confused, I changed your sample to:

作为编译器相当混乱的证据,我将您的示例更改为:

Public Interface ICurrentStep
    Property outerstep() As String = "Hello"
    Property innerstep() As String
End Interface

This should produce an error about not being allowed an initializer in an interface. However, instead of just two error messages stating "Visual Basic 9.0 does not support auto-implemented properties." we alsoget the error "Expanded Properties cannot be initialized.". But, this does not make sense:

这应该会产生关于不允许在接口中使用初始值设定项的错误。但是,不是只有两条错误消息指出“Visual Basic 9.0 不支持自动实现的属性”。我们收到错误“无法初始化扩展属性。”。但是,这没有意义

there are situations in which you cannot use an auto-implemented property and must instead use standard, or expanded, property syntax.

在某些情况下,您不能使用自动实现的属性,而必须使用标准或扩展的属性语法。

That is, a single property can eitherbe auto-implemented or expanded.

也就是说,一个单一的财产可以要么是自动实现的或已展开。



My recommendation would be to move as much code as possible out of App_Code- either just outside of it or into a different library. This will then mean that the code is actually compiled by the Roslyn compiler directly (and without the /langversionsetting) and that you can actually start making using of modern VB features (you can still target v3.5 but use later languagefeatures)

我的建议是将尽可能多的代码移出App_Code- 无论是在它之外还是移到不同的库中。这将意味着代码实际上是由 Roslyn 编译器直接编译的(并且没有/langversion设置),并且您实际上可以开始使用现代 VB 功能(您仍然可以针对 v3.5,但使用更高版本的语言功能)

In the alternative, you can leave the code in App_Codeand choose to ignore the errors. If it's just two errors, that may be feasible in the short term.

或者,您可以保留代码App_Code并选择忽略错误。如果只是两个错误,那在短期内可能是可行的。

回答by Rafael Neto

In my case, using VS2015 Community, the real problem was a blank line between the property header and the Getmethod. See my screenshots below:

就我而言,使用 VS2015 社区,真正的问题是属性标题和Get方法之间的空行。请参阅下面的屏幕截图:

Before: (Error image)

之前:(错误图像

enter image description here

在此处输入图片说明

After: (No error image)

之后:(没有错误图像

enter image description here

在此处输入图片说明

回答by Terry Gibbs

I have a project that is in the process of moving to use VS2015 from VS2008. We need to support both environments in the short term so I have created VS015 .vbproj files that include the directive 9 in the PropertyGroup section of the file.

我有一个项目正在从 VS2008 转向使用 VS2015。我们需要在短期内支持这两种环境,因此我创建了 VS015 .vbproj 文件,其中在文件的 PropertyGroup 部分包含指令 9。

I was getting this error because there was a comment between the Property declaration and the Get clause. For example

我收到此错误是因为 Property 声明和 Get 子句之间有注释。例如

Private ReadOnly Property MemberNode() As XmlNode</br>
    ' This is the only place which locates the objMember node
    Get
        Return CreatePathAndNode(mnodMessage, "objData/colMember/objMember")
    End Get 
End Property

Moving the comment before the "Private Readonly" line removed the compiler error in VS2015.

移动“Private Readonly”行之前的注释消除了 VS2015 中的编译器错误。

回答by GDavoli

This may seems trivial and OT, but if you're using Visual Studio Community 2015 and you're targeting .net 3.5 make sure the line continuation "_" has not been automatically removed.

这可能看起来微不足道,但如果您使用的是 Visual Studio Community 2015 并且您的目标是 .net 3.5,请确保未自动删除行继续符“_”。

VSC 2015 automatically removes the line continuation upon edit and there's no way to reinsert it back (I had to do it outside of VSC 2015). Furthermore the compiler gives you an error on that line but the editor does not underline it.

VSC 2015 在编辑时自动删除行延续,并且无法重新插入它(我必须在 VSC 2015 之外执行此操作)。此外,编译器在该行上给您一个错误,但编辑器没有给它加下划线。

回答by GDavoli

In my case (VS2015, ASP.NET app, VB.NET, .NET 4.6) I've solved it by removing the space between the procedure declaration and the Get. The snippet below raises the "Visual Basic 12.0 does Not support ReadOnly auto-implemented properties."

就我而言(VS2015、ASP.NET 应用程序、VB.NET、.NET 4.6),我已经通过删除过程声明和 Get 之间的空格解决了这个问题。下面的代码段引发了“Visual Basic 12.0 不支持 ReadOnly 自动实现的属性”。

Public ReadOnly Property TestProp() As Integer

    Get
        Return 0
    End Get
End Property

The snippet below does not.

下面的片段没有。

Public ReadOnly Property TestProp() As Integer
    Get
        Return 0
    End Get
End Property

回答by Jim Marshall

Public Interface ICurrentStep
    Private _outerstep As String
    Property outerstep() As String
        Get
            Return _outerstep
        End Get
        Set(ByVal value As String)
            _outerstep = value
        End Set
    End Property
End Interface

回答by Xaphod

The build error is a bug in the compiler and is also in Visual Studio 2015 RTM. Installing update to Visual Studio 2015 RTM solved this issue for me.

构建错误是编译器中的一个错误,也在 Visual Studio 2015 RTM 中。安装 Visual Studio 2015 RTM 更新为我解决了这个问题。

(Tested and verified with Visual Studio 2015 RTM Update 1 and Update 3.)

(使用 Visual Studio 2015 RTM Update 1 和 Update 3 进行测试和验证。)