从所有值的子集创建匿名枚举值 [英] Create anonymous enum value from a subset of all values
本文介绍了从所有值的子集创建匿名枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有一个枚举类型定义为:
Let's say we have an enum type defined as:
enum Statuses
{
Completed,
Pending,
NotStarted,
Started
}
我想让Autofixture为我创建一个值,例如待处理.
I'd like to make Autofixture create a value for me other than e.g. Pending.
因此(假设是循环生成),我想获得:
So (assuming round-robin generation) I'd like to obtain:
已完成,未启动,已启动,已完成,未启动,...
Completed, NotStarted, Started, Completed, NotStarted, ...
推荐答案
最简单的方法是使用AutoFixture的 Generator< T>
:
The easiest way to do that is with AutoFixture's Generator<T>
:
var statuses = fixture
.Create<Generator<Statuses>>()
.Where(s => Statuses.Pending != s)
.Take(10);
如果您只需要一个值,但要确保它不是 Statuses.Pending
,则可以执行以下操作:
If you only need a single value, but want to be sure that it's not Statuses.Pending
, you can do this:
var status = fixture
.Create<Generator<Statuses>>()
.Where(s => Statuses.Pending != s)
.First();
也有其他方法,但这是即席查询最简单的方法.
There are other ways, too, but this is the easiest for an ad-hoc query.
这篇关于从所有值的子集创建匿名枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文