c# - 使用 msbuild 14/VS 2015 干净地构建的简单代码,但无法在 msbuild 15/VS 2017 中编译

标签 c# visual-studio-2015 visual-studio-2017

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 中的显示方式如下:

enter image description here

最佳答案

我认为它能够在 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.

正式语法是:

enter image description here

您发布的语法 [MyAttribute1 MyAttribute2] 未被考虑在内。

所以,我认为这是一个能够编译的错误,他们在 msbuild 15 中修复了它。

关于c# - 使用 msbuild 14/VS 2015 干净地构建的简单代码,但无法在 msbuild 15/VS 2017 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525087/

相关文章:

c# - 如何在UWP中的类库项目中使用Resources.resw?

c# - 如何在 VS 2017 for ASP.NET Core 项目中按路径段嵌套文件

c# - 如何检查辅助角色是否正在 Azure 模拟器中运行

C++ REST (Casablanca) - web::json::value 没有名为 'field_map' 的成员

"None"的 TypeScript 构建模块系统不会继续存在

c# - 如何在 Visual Studio 中测量 ASP.NET Core 项目中的代码覆盖率?

typescript - 将 Typescript 更新为 v3.2.1,SDK 文件夹中没有 tsc.exe

msbuild - VS2017中的新项目可以将nuget包存储在项目包目录中吗?

c# - 如何模拟(使用 Moq)由 Newtonsoft.json 序列化的接口(interface)?

c# - C# 的托管与非托管物理引擎