[英]List of Enums C#
Am using an api where I need to set a list of days in a week, there is a enum for these days but I cannot use List. 我正在使用需要在一周中设置几天列表的api,这些天有一个枚举,但是我不能使用列表。 The parameter is expecting something like this RecurringDay.Monday |
该参数期望像这样的RecurringDay.Monday | RecurringDay.Wednesday
循环日星期三
I cannot seem to find a means of building this up with the values I have for my days without doing it in a way that seems very poor coding. 我似乎找不到一种方法,可以用我当日拥有的值来构建它,而不必以一种看起来非常糟糕的编码方式进行。 How can I construct a list of days a user selects like RecurringDay.Monday |
如何构建用户选择的日期列表,如RecurringDay.Monday | RecurringDay.Wednesday |
RecurringDay.Wednesday | RecurringDay.Friday
RecurringDay.Friday
[Flags]
public enum RecurringDay
{
RecurringNone = 0,
RecurringSunday = 1,
RecurringSaturday = 2,
RecurringWeekend = 3,
RecurringFriday = 4,
RecurringThursday = 8,
RecurringWednesday = 16,
RecurringTuesday = 32,
RecurringMonday = 64,
RecurringWeekdays = 124,
RecurringAlldays = 127
}
schedule.LocalTime = new HueDateTime()
{
DateTime = DateTime.Now.AddMinutes(1),
RecurringDay = RecurringDay.RecurringMonday | RecurringDay.RecurringAlldays
};
Flags enums are bit vectors. 标志枚举是位向量。 Each enum value corresponds to a bit in the underlying integer.
每个枚举值对应于基础整数中的一位。 Combinations of set bits represents combinations of values.
设置位的组合表示值的组合。 For this to work every value need to be a power of 2, ie exactly on bit is set.
为此,每个值都必须是2的幂,即正好设置了位。 But combinations of bits may also have names, like
RecurringWeekend
in the above example. 但是位的组合也可能具有名称,例如上例中的
RecurringWeekend
。
Example: 例:
0b00010001 = 0x11 = 17
1 Sunday included
0 Saturday not included
0 Friday not included
0 Thursday not included
1 Wednesday included
0 Tuesday not included
0 Monday not included
To build up such a combination just use the |= operator: 要建立这样的组合,只需使用| =运算符:
RecurringDay days = RecurringDay.RecurringNone;
if (user_selected_Sunday)
days |= RecurringDay.RecurringSunday;
if (user_selected_Saturday)
days |= RecurringDay.RecurringSaturday;
...
if (user_selected_Monday)
days |= RecurringDay.RecurringMonday;
At the end day
will contain the appropriate combination of days matching the users selection. 最后
day
将包含与用户选择匹配的适当天数组合。
In fact every instance of a flags enum value is already a set of values which of course might also be empty, ie RecurringDay.RecurringNone
. 其实一个标志枚举值的每个实例都已经是一组值这当然也可能是空的,即
RecurringDay.RecurringNone
。 No need to use List<>
here. 无需在此处使用
List<>
。
I'm not sure what your question is, but this code looks fine to me. 我不确定您的问题是什么,但是这段代码对我来说看起来不错。 First, I got rid of your recurring "Recurring", and made the combinations more clear (as @Heretic Monkey suggested):
首先,我摆脱了您重复出现的“重复出现”,并使组合更清晰(如@Heretic Monkey建议):
[Flags]
public enum RecurringDay
{
None = 0,
Sunday = 1,
Saturday = 2,
Friday = 4,
Thursday = 8,
Wednesday = 16,
Tuesday = 32,
Monday = 64,
Weekends = Saturday | Sunday,
Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday,
Alldays = Weekdays | Weekends,
}
Then I recreated your HueDateTime class with property names that weren't type names (for clarity): 然后,我使用不是类型名称的属性名称(为清楚起见)重新创建了HueDateTime类:
public class HueDateTime
{
public DateTime When { get; set; }
public RecurringDay HowOften { get; set; }
}
Then I consumed the code: 然后我使用了代码:
var schedule = new HueDateTime()
{
When = DateTime.Now + TimeSpan.FromHours(1),
HowOften = RecurringDay.Monday | RecurringDay.Wednesday
};
var succeeded = Enum.TryParse<RecurringDay>("Sunday,Monday,Tuesday", out var when);
When that last statement finishes executing, succeeded
is true
and when
is typed as a RecurringDay
with a value of Sunday | Tuesday | Monday
当这最后语句执行完毕,
succeeded
是true
和when
的类型是RecurringDay
具有的价值Sunday | Tuesday | Monday
Sunday | Tuesday | Monday
Sunday | Tuesday | Monday
. Sunday | Tuesday | Monday
。 I'm pretty sure this code could easily pass a picky code reviewer. 我非常确定此代码可以轻松地通过挑剔的代码审阅者。
I just noticed, your days go backwards (Sunday, Saturday ... Tuesday, Monday) - any particular reason? 我刚刚注意到,您的日子倒退了(星期日,星期六,星期二,星期一)-出于某些原因? That's why
when
ends up as Sunday | Tuesday | Monday
这就是为什么
when
Sunday | Tuesday | Monday
结束的原因Sunday | Tuesday | Monday
Sunday | Tuesday | Monday
Sunday | Tuesday | Monday
. Sunday | Tuesday | Monday
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.