我有一个使用枚举将位掩码转换为天数列表的方法。我试图做相反的事情,但遇到了麻烦。我有DaysOfWeekToEnum
工作,但没有DaysOfWeekFromEnum
。以下是我正在尝试做的事情。任何人都可以帮忙吗?
public static short DaysOfWeekFromEnum(IEnumerable<DaysInWeekIds> daysOfWeek)
{
short mask;
foreach (var item in daysOfWeek)
{
mask &= item; // ????
}
return mask
}
public static IEnumerable<DaysInWeekIds> DaysOfWeekToEnum(short mask)
{
var values = new List<DaysInWeekIds>();
foreach (short enumValue in Enum.GetValues(typeof(DaysInWeekIds)))
{
if (mask & enumValue == enumValue)
{
values.Add((DaysInWeekIds) Enum.ToObject(typeof(DaysInWeekIds),
enumValue));
}
}
return values;
}
/// <summary> The days in week ids. </summary>
public enum DaysInWeekIds : short
{
M = 1,
Tu = 2,
W = 4,
Th = 8,
F = 16,
Sa = 32,
Su = 64
}