Reputation: 17578
Take the following simple code. The DoMyCode
method has two attributes applied to it, with a space between the entries.
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.
If you run this code in Visual Studio 2015 (update 3) it will compile and run with no errors or warnings.
However, running the exact same code in Visual Studio 2017 will produce the following exception:
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:
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?
For full context, I am experimenting with using VS 2017 on my team's unit test projects. 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.
Upvotes: 10
Views: 713
Reputation: 17578
Thanks to Hans Passant who shared this information.
It appears that this was indeed a bug with the roslyn compiler. It was reported in October of 2016, well after the last release of VS 2015 in June of the same year.
This fix made its way into Visual Studio 2017, which is why this syntax is properly reported as an error.
Upvotes: 11