c# - 属性不能在 C++/CLI 中重复但在 C# 中可以吗?

标签 c# c++-cli xunit.net

我收到 error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated 在 C++/CLI 代码中,但不是 C#。

xUnit.net看起来像是我祈祷的答案——一个带有 GUI 的现代单元测试框架,可以与 C++/CLI 一起工作。但是,使用他们的参数化测试方法给我错误 C3095,如下所示。

有什么想法吗?

我正在使用最新的 xUnit.net 1.6 和 Visual Studio 2008SP1。

using namespace Xunit;
using namespace Xunit::Extensions;

public ref class ParameterisedTestClass
{
public:

    [Theory]
    [InlineData("Kilroy", 6)]
    // uncomment to cause c3095 [InlineData("Jones", 5)]
    void PropTest(String^ msg, int msgLen)
    {
        Assert::Equal(msg->Length, msgLen);
    }
};

C# 中的等价物很好

using Xunit;
using Xunit.Extensions;

public  class ParameterisedTestClass
{

    [Theory]
    [InlineData("Kilroy", 6)]
    [InlineData("Jones", 5)]
    public void PropTest(String msg, int msgLen)
    {
        Assert.Equal(msg.Length, msgLen);
    }
};

最佳答案

我的猜测是这是由于继承,并且其中一个编译器弄错了。

InlineDataAttribute继承自 DataAttribute .现在 DataAttribute 声明为允许多个实例:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]

但是 InlineDataAttribute 本身没有任何明确的 AttributeUsage 属性。我怀疑 C++ 编译器没有发现继承的 AllowMultiple... 或者它可能不应该 被继承。我找不到任何关于 AttributeUsageAttribute 继承的详细文档。本身 - 虽然它有 Inherited=true,所以我猜它应该完全被 InlineDataAttribute 继承......

关于c# - 属性不能在 C++/CLI 中重复但在 C# 中可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3116911/

相关文章:

c# - 将 DataGrid 的一行传递给另一个 wpf 表单 c#

c# - 在不同的用户上安装 PFX 证书

c++-cli - 如何将数据从非托管代码推送到托管代码?

c# - 如何使 xUnit 并行运行 Theory?

c# - 扩展 xUnit.NET 以在处理类和定位测试方法时使用自定义代码

c# - 通过代码向 WPF 功能区添加新项目

c# - 设置 [PARAMETER] 的值时意外结束 - Visual Studio 说字符串不是有效的 JSON,而在线验证器说它是

c# - 使用 C++/CLI 而不是 C# 来包装(第三方)C 库 (dll) 有什么优势?

c++ - 复制结构时出现 System.AccessViolationException

c# - 如何测试 Web API JSON 响应?