简体   繁体   English

为什么VS 2015允许以空格分隔的属性,而VS 2017则不允许?

[英]Why does VS 2015 allow attributes separated by spaces while VS 2017 does not?

Take the following simple code. 请使用以下简单代码。 The DoMyCode method has two attributes applied to it, with a space between the entries. DoMyCode方法应用了两个属性,条目之间有空格。

using System;

namespace AttributeSpaceTesting
{
    class Program
    {
        static void Main(string[] args) { }

        [Horse Cow]
        static void DoMyCode() { }
    }

    [AttributeUsage(AttributeTargets.Method)]
    class HorseAttribute : Attribute { }

    [AttributeUsage(AttributeTargets.Method)]
    class CowAttribute : Attribute { }
}

This was made in VS 2015 targeting .Net 4.6. 这是在VS 2015中针对.Net 4.6制作的。

If you run this code in Visual Studio 2015 (update 3) it will compile and run with no errors or warnings. 如果您在Visual Studio 2015(更新3)中运行此代码,它将编译并运行,没有错误或警告。

However, running the exact same code in Visual Studio 2017 will produce the following exception: 但是,在Visual Studio 2017中运行完全相同的代码将产生以下异常:

Syntax error, ',' expected

I understand why this error happens. 我理解为什么会发生这种错误。 I expect that you would need commas between attribute items. 我希望你在属性项之间需要逗号。 In section 17.2 of the C# 5 specifications: 在C#5规范的第17.2节中:

An attribute section consists of a pair of square brackets, which surround a comma-separated list of one or more attributes. 属性部分由一对方括号组成,它们围绕一个或多个属性的逗号分隔列表。

So why is this syntax legal in VS 2015? 那么为什么这种语法在VS 2015中合法呢?


For full context, I am experimenting with using VS 2017 on my team's unit test projects. 对于完整的上下文,我正在尝试在我的团队的单元测试项目中使用VS 2017。 I've noticed lines like this: 我注意到这样的线条:

[Description(@"The Test Description")]
[TestCategory("Regression"), TestCategory("Module"), TestCategory("ModuleRegression") TestMethod]
public void TC_12345_VerifyTheThingWorks()

which cause syntax errors when compiled in the new Visual Studio system. 在新的Visual Studio系统中编译时会导致语法错误。

Thanks to Hans Passant who shared this information. 感谢Hans Passant分享了这些信息。

It appears that this was indeed a bug with the roslyn compiler . 看来这确实是roslyn编译器的一个错误 It was reported in October of 2016, well after the last release of VS 2015 in June of the same year. 据报道,2016年10月,也就是同年6月VS 2015的最后一次发布。

This fix made its way into Visual Studio 2017, which is why this syntax is properly reported as an error. 此修复程序已进入Visual Studio 2017,这就是为什么此语法被正确报告为错误的原因。

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

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