.NET Core netstandard 1.2 class 库中不存在 BindingFlags

BindingFlags does not exist in .NET Core netstandard 1.2 class library

在同时针对 net452netstandard1.3 框架的 .NET Core class 库项目中,我试图将后者向后移动到 netstandard1.2 以便扩展向后兼容性。

项目使用 BindingFlags 枚举并在 net452 和 netstandard1.3 上构建良好,但它在 netstandard1.2 上失败并出现许多错误,所有错误看起来都像:

CS0103 The name 'BindingFlags' does not exist in the current context

或喜欢:

CS7069 Reference to type 'BindingFlags' claims it is defined in 'System.Reflection', but it could not be found

目前,project.json 中的 frameworks 属性是:

"frameworks": {
  "netstandard1.2": {
    "imports": [
      "dnxcore50",
    ],
    "dependencies": {
      "NETStandard.Library": "1.6.0",
      "runtime.any.System.Collections": "4.0.11",
      "System.Collections": "4.0.11",
      "System.Collections.NonGeneric": "4.0.1",
      "System.Console": "4.0.0",
      "System.IO.FileSystem": "4.0.1",
      "System.Reflection.TypeExtensions": "4.1.0",
      "System.Runtime.Serialization.Xml": "4.1.1",
      "System.Text.Encodings.Web": "4.0.0"
    }
  },
  "net452": {
    "frameworkAssemblies": {
      "System.IO": "",
      "System.Runtime.Serialization.Xml": "",
      "System.Xml": "",
      "System.Web": ""
    }
  }
},

并且它已经列出了来自 System.Reflection.TypeExtensions 的依赖项。

显示潜在修复程序 弹出窗口建议安装 "System.Reflection": "4.3.0-preview1-24530-04",却发现该类型仍然显示为红色波浪线。另一个 显示潜在的修复程序 弹出窗口第二次建议安装 "System.Reflection.TypeExtensions": "4.3.0-preview1-24530-04"。当然在不影响报错的情况下,还是有的。

构建 net452 端正常,错误仅出现在 netstandard1.2 构建上。

这种 BindingFlags 类型有什么问题?

根据 netstandard 1.5 diffBindingFlags 枚举已添加到 1.5 版本的标准中。

之前它可以通过 System.Reflection.TypeExtensions 包获得,该包仅支持 1.3 或更高版本,因此定位 1.2 不起作用。