using System;
namespace ConsoleApplication1
{
[AttributeUsage(AttributeTargets.Class)]
public class MyAttribute1 : Attribute
{
}
[AttributeUsage(AttributeTargets.Class)]
public class MyAttribute2 : Attribute
{
}
[MyAttribute1 MyAttribute2]
class Program
{
static void Main(string[] args)
{
}
}
}
请注意,应用于程序的属性之间没有逗号。我非常惊讶地发现它可以使用 msbuild 14/VS 2015 进行编译,但无法使用 msbuild 15/VS 2017 进行编译。
我的期望是看到它在两个版本中都失败了。
谁能解释一下这是怎么回事?
编辑 1
反射代码在 Reflector 中的显示方式如下:
最佳答案
我认为它能够在 msbuild14 中编译是一个错误。
C# v5 语言 specification (第 394 页)指出:
Attributes are specified in attribute sections . An attribute section consists of a pair of square brackets, which surround a comma - separated list of one or more attributes. The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B] , [B][A] , [A, B] , and [B, A] are equivalent.
正式语法是:
您发布的语法 [MyAttribute1 MyAttribute2]
未被考虑在内。
所以,我认为这是一个能够编译的错误,他们在 msbuild 15 中修复了它。
关于c# - 使用 msbuild 14/VS 2015 干净地构建的简单代码,但无法在 msbuild 15/VS 2017 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525087/