隐式类型的三元组数组

发布于 2024-10-06 20:36:33 字数 476 浏览 2 评论 0原文

我有一个单元测试方法:

private bool TestCompatibility(string type1, string type2, bool shouldBeCompatible)
{
}

因为它“知道”哪些类型(设计)兼容,所以它调用正在测试的单元并查找错误。仅当类型不兼容时才会出现错误,因此方法测试单元类型检查代码是否正确实现。

问题:我如何编写三胞胎集合?

我想要类似的东西:

var ar = { { "Num", "Num", true }, { "Num", "Datetime", false } };
foreach (var triplet in ar)
{
    // ???
}

使用隐式类型。

PS 我知道我可以将属性与 NUnit 一起使用。尽管如此,我想看看如何在没有库的情况下编写它。

问候,

I have a unit test method:

private bool TestCompatibility(string type1, string type2, bool shouldBeCompatible)
{
}

As it "knows" which types are (designed) compatible, it makes a call to the unit that is being tested and looks for errors. Errors should appear for incompatible types only, so the method tests, is units type-checking code right-implemented or not.

The question: how have I write the triplets collection?

I want something like:

var ar = { { "Num", "Num", true }, { "Num", "Datetime", false } };
foreach (var triplet in ar)
{
    // ???
}

with implicit typing.

P.S. I know that I can use attributes along with NUnit. Nevertheless, I want to see, how it can be written without libraries.

Regards,

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

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

发布评论

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

评论(1

寂寞陪衬 2024-10-13 20:36:33

我不知道这是否是您正在寻找的,但您可以使用匿名类型:

var ar = new[] { 
    new { Type1 = "Num", Type2 = "Num", Compatible = true }, 
    new { Type1 = "Num", Type2 = "Datetime", Compatible = false } 
};
foreach (var triplet in ar)
{
    TestCompatibility(triplet.Type1, triplet.Type2, triplet.Compatible);
}

I don't know if this is what you are looking for, but you could make use of anonymous types:

var ar = new[] { 
    new { Type1 = "Num", Type2 = "Num", Compatible = true }, 
    new { Type1 = "Num", Type2 = "Datetime", Compatible = false } 
};
foreach (var triplet in ar)
{
    TestCompatibility(triplet.Type1, triplet.Type2, triplet.Compatible);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文