不使用Xunit.Sdk.DataAttribute抽象类实现的成员

本文关键字:抽象类 实现 成员 DataAttribute Sdk Xunit | 更新日期: 2023-09-27 18:16:40

我试图实现Xunit.Sdk.DataAttribute抽象类,但由于某种原因它不工作。

public sealed class CustomDataAttribute: Xunit.Sdk.DataAttribute
{
    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
    {
        throw new NotImplementedException();
    }
}

它说:抽象继承成员

' IEnumerable<object[]> Xunit.Sdk.DataAttribute.GetData(System.Reflection.MethodInfo) '

未实现

那么它显然被实现了。

此错误只能在一个项目中重现,在其他项目中它可以正常工作。也许你也有类似的情况?

不使用Xunit.Sdk.DataAttribute抽象类实现的成员

这似乎是新工具中的一个缺陷,可以通过在project.jsonframeworkAssemblies部分添加对System.ReflectionSystem.Runtime的构建时引用来解决,使其看起来像这样:

{
  "version": "1.0.0-*",
  "dependencies": {
    "YourAssemblyUnderTest": "1.0.0-*",
    "xunit":  "2.*" 
  },
  "frameworks": {
    "net45": {
      "frameworkAssemblies": {
        "mscorlib": "",
        "System": "",
        "System.Core": "",
        "System.Web": "",
        "System.Reflection": {
          "type": "build",
          "version": ""
        },
        "System.Runtime": {
          "type": "build",
          "version": ""
        }
      }
    }
  }
}

这个问题正在xUnit issue #618和#716上被跟踪,Axel Heer写了一篇关于它的好文章,尽管是用德语写的。总结一下Axel的帖子,如果我翻译错了,我道歉,德语不是我的母语:

基于PCL库创建DotNet Core库并不像引入DotNet Core之前那样顺利。在某些情况下,包含在依赖项的依赖项中的类型不可用,这会导致编译器错误,例如:

  • MyDataAttribute不实现继承的抽象成员DataAttribute。GetData (MethodInfo)
  • MyDataAttribute。GetData (MethodInfo):没有找到合适的方法重写

通过引用System.ReflectionSystem.Runtime,我们显式地引用了这些依赖项。