4 回答
TA贡献1803条经验 获得超3个赞
由于您的枚举使用与内置相同的天数顺序DayOfWeek,因此您需要做的就是使用DayOfWeektype 的变量作为的指数2,然后将其与您的枚举变量按位与运算。
像这样的东西(这将检查Monday你的枚举标志1是否存在):
MyDayOfWeek Days = MyDayOfWeek.Monday | MyDayOfWeek.Friday;
DayOfWeek D = DayOfWeek.Monday;
var Mask = 1 << (int)D;
if ((Mask & (int)Days) == Mask)
//Do whatever;
我已将您的枚举重命名为MyDaysOfWeek,而DayOfWeek是内置 .NET 类型。你可以在一周中的任何一天做同样的事情。
TA贡献1784条经验 获得超8个赞
最后,我有解决方案
拥有带有标志的 DaysOfWeek:
[Flags]
public enum DaysOfWeek
{
None = 0,
Sunday = 1 << 0,
Monday = 1 << 1,
Tuesday = 1 << 2,
Wednesday = 1 << 3,
Thursday = 1 << 4,
Friday = 1 << 5,
Saturday = 1 << 6,
}
由于自己的枚举具有相同的天数,我们可以编写扩展方法将标准 DayOfWeek 转换为自己的
public static class EnumExtensions
{
public static DaysOfWeek ToFlag(this DayOfWeek dayOfWeek)
{
var mask = 1 << (int)dayOfWeek;
return (DaysOfWeek)Enum.ToObject(typeof(DaysOfWeek), mask);
}
}
和用法:
var days = DaysOfWeek.Sunday | DaysOfWeek.Friday;
var day = DayOfWeek.Sunday;
var ownDay = day.ToFlag();
if (days.HasFlag(ownDay))
// Do whatever;
TA贡献1876条经验 获得超6个赞
public enum BitwiseDayOfWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
public class Program
{
public static void Main(string[] args)
{
BitwiseDayOfWeek scheduledDayOfWeek
= BitwiseDayOfWeek.Saturday | BitwiseDayOfWeek.Sunday;
// turn System.DayOfWeek (DateTime.Now.DayOfWeek) into BitwiseDayOfWeek
BitwiseDayOfWeek currentDayOfWeek
= (BitwiseDayOfWeek)Math.Pow(2, (double)DateTime.Now.DayOfWeek);
// test if today is the scheduled day
if ( (currentDayOfWeek & scheduledDayOfWeek) == currentDayOfWeek )
Console.WriteLine(currentDayOfWeek);
Console.WriteLine("---------------------");
Console.ReadLine();
}
}
TA贡献1810条经验 获得超5个赞
如果您像这样更改枚举:
[Flags]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
你可以试试这个:
class Program
{
public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)
{
int mineInt = (int)mine;
int cSharpInt = (int)cSharp;
return mineInt == cSharpInt;
}
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
DayOfWeek dayOfWeek = DayOfWeek.Sunday;
bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}
如果你不能改变你的枚举,你可以试试这个:
class Program
{
public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)
{
if (mine == DayOfWeek.Friday && cSharp == System.DayOfWeek.Friday ||
mine == DayOfWeek.Monday && cSharp == System.DayOfWeek.Monday ||
mine == DayOfWeek.Saturday && cSharp == System.DayOfWeek.Saturday ||
mine == DayOfWeek.Sunday && cSharp == System.DayOfWeek.Sunday ||
mine == DayOfWeek.Thursday && cSharp == System.DayOfWeek.Thursday ||
mine == DayOfWeek.Tuesday && cSharp == System.DayOfWeek.Tuesday ||
mine == DayOfWeek.Wednesday && cSharp == System.DayOfWeek.Wednesday)
return true;
return false;
}
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
DayOfWeek dayOfWeek = DayOfWeek.Tuesday;
bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}
- 4 回答
- 0 关注
- 127 浏览
添加回答
举报