3
我有一個方法,使用Enum將位掩碼轉換爲日期列表。我試圖做相反的事情,但遇到麻煩。我有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
}
根據您的.NET版本,'.ToArray()'可能是不必要的。 – 2012-08-05 13:55:33
@TimS。這是真的 - 我添加了'ToArray()'使它在單聲道的ideone上編譯。 – dasblinkenlight 2012-08-05 13:56:48