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

发布于 2024-09-07 12:32:45 字数 1010 浏览 2 评论 0原文

我在 C++/CLI 代码中收到 error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute can berepeated,但在 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);
    }
};

I'm getting error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated in C++/CLI code but not C#.

xUnit.net looks like the answer to my prayers - a modern unit test framework with GUI working with C++/CLI. However, using their approach to parameterised testing gives me the error C3095 as shown below.

Any ideas?

I'm using the latest xUnit.net 1.6 with 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);
    }
};

the equivalent in C# is fine

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);
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凹づ凸ル 2024-09-14 12:32:45

我的猜测是,这是由于继承造成的,并且编译器之一出错了。

InlineDataAttribute 继承自 数据属性。现在,DataAttribute 被声明为允许多个实例:

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

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

My guess is that this is due to inheritance, and one of the compilers getting it wrong.

InlineDataAttribute inherits from DataAttribute. Now DataAttribute is declared to allow multiple instances:

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

but InlineDataAttribute doesn't have any explicit AttributeUsage attribute itself. I suspect the C++ compiler isn't spotting the inherited AllowMultiple... or it's possible that it shouldn't be inherited. I can't find any detailed documentation about the inheritance of AttributeUsageAttribute itself - although it has Inherited=true, so I guess it should be inherited completely by InlineDataAttribute...

メ斷腸人バ 2024-09-14 12:32:45

嗯...我查看了 defs 这里此处,并在下面复制它们(删减);通过 DataAttribute 继承 AllowMultiple 在 C# 中工作正常:

class Test
{
    [InlineData]
    [InlineData]
    static void Main() { }
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
class DataAttribute : Attribute {}

class InlineDataAttribute : DataAttribute { }

因此,如果它不适用于 C++/CLI,我猜 C++/CLI 根本无法处理隐含的 <代码>[属性用法]。您应该向 Xunit 提出请求,要求他们在 InlineDataAttribute 上显式显示 [AttributeUsage]

Hmmm... I looked at the defs here and here, and reproduced them (cut-down) below; the inheritance of AllowMultiple via DataAttribute works fine in C#:

class Test
{
    [InlineData]
    [InlineData]
    static void Main() { }
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
class DataAttribute : Attribute {}

class InlineDataAttribute : DataAttribute { }

So if it isn't working for C++/CLI, I guess C++/CLI simply isn't processing the implied [AttributeUsage]. You should file a request against Xunit asking them to make the [AttributeUsage] explicit on InlineDataAttribute.

给我一枪 2024-09-14 12:32:45

我用这个 C++/CLI 片段重现:

[AttributeUsage(AttributeTargets::All, AllowMultiple = true, Inherited = true)]
ref class BaseAttribute : Attribute {};

ref class DerivedAttribute : BaseAttribute {};

[Derived]
[Derived]    // Error C3095
ref class Test {};

显然,这是 C++/CLI 编译器中的一个错误。我在 connect.microsoft.com 上没有看到相关报道。自己这样做可能不值得付出努力,该语言处于维护模式。

可能的解决方法是编辑 xUnit 的源代码以再次分配AllowMultiple 或创建您自己的继承InlineDataAttribute 的InlineDataFixedAttribute。

I repro with this C++/CLI snippet:

[AttributeUsage(AttributeTargets::All, AllowMultiple = true, Inherited = true)]
ref class BaseAttribute : Attribute {};

ref class DerivedAttribute : BaseAttribute {};

[Derived]
[Derived]    // Error C3095
ref class Test {};

Clearly this is a bug in the C++/CLI compiler. I don't see it reported at connect.microsoft.com. Doing so yourself probably isn't worth the effort, the language is in maintenance mode.

Possible workarounds are editing the source code of xUnit to assign AllowMultiple again or to create your own InlineDataFixedAttribute that inherits InlineDataAttribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文