لا يمكن تكرار السمة في C ++/CLI ولكن موافق في C#؟

StackOverflow https://stackoverflow.com/questions/3116911

  •  29-09-2019
  •  | 
  •  

سؤال

انا احصل error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated في رمز C ++/CLI ولكن ليس C#.

xunit.net يبدو أن الإجابة على صلاتي - إطار اختبار الوحدة الحديث مع واجهة المستخدم الرسومية يعمل مع 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);
    }
};
هل كانت مفيدة؟

المحلول

هممم ... نظرت إلى defs هنا و هنا, ، واستنسخها (قطع) أدناه ؛ ميراث AllowMultiple عبر DataAttribute يعمل بشكل جيد في 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 ببساطة لا يعالج الضمني [AttributeUsage]. يجب عليك تقديم طلب مقابل Xunit يطلب منهم عمل [AttributeUsage] صريح على InlineDataAttribute.

نصائح أخرى

أظن أن هذا يرجع إلى الميراث ، وأن أحد المترجمين يخطئون.

InlineDataAttribute يرث من DataAttribute. حاليا DataAttribute يُعلن أنه يسمح بحالات متعددة:

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

لكن InlineDataAttribute ليس لديه أي صريح AttributeUsage تنسب نفسها. أنا مشتبه فيه لا يقوم برنامج التحويل البرمجي C ++ باكتشاف السماح الموروثة ... أو من الممكن أن يكون ذلك لا ينبغي تكون موروثة. لا يمكنني العثور على أي وثائق مفصلة حول ميراث AttributeUsageAttribute نفسها - على الرغم من أنها لديها Inherited=true, ، لذلك أعتقد ذلك ينبغي ورثت تماما من قبل InlineDataAttribute...

أنا أعمدة مع هذا المقتطف 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 لتعيين Loomultiple مرة أخرى أو لإنشاء inlinedatafixedattribute الذي يرث inlinedataattribute.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top