如何将标准 DayOfWeek 与 Flag 进行比较

我有自己的 DaysOfWeek Flag 枚举(类似于https://learn.microsoft.com/en-us/previous-versions/ms886541(v=msdn.10)

[Flags]

public enum DayOfWeek

{

    Sunday = 0,

    Monday = 1,

    Tuesday = 2,

    Wednesday = 4,

    Thursday = 8,

    Friday = 16,

    Saturday = 32

}

我需要将标准的 DayOfWeek 与我的进行比较。我怎样才能做到这一点?


撒科打诨
浏览 128回答 4
4回答

繁星点点滴滴

由于您的枚举使用与内置相同的天数顺序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 类型。你可以在一周中的任何一天做同样的事情。

青春有我

最后,我有解决方案拥有带有标志的 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;

HUX布斯

public enum BitwiseDayOfWeek{&nbsp; &nbsp; Sunday&nbsp; &nbsp;= 1,&nbsp; &nbsp; Monday&nbsp; &nbsp;= 2,&nbsp; &nbsp; Tuesday&nbsp; = 4,&nbsp; &nbsp; Wednesday&nbsp; &nbsp; = 8,&nbsp; &nbsp; Thursday&nbsp; &nbsp; &nbsp;= 16,&nbsp; &nbsp; Friday&nbsp; &nbsp;= 32,&nbsp; &nbsp; Saturday&nbsp; &nbsp; &nbsp;= 64}public class Program{&nbsp; &nbsp; public static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; BitwiseDayOfWeek scheduledDayOfWeek&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = BitwiseDayOfWeek.Saturday | BitwiseDayOfWeek.Sunday;&nbsp; &nbsp; &nbsp; &nbsp; // turn System.DayOfWeek (DateTime.Now.DayOfWeek) into BitwiseDayOfWeek&nbsp; &nbsp; &nbsp; &nbsp; BitwiseDayOfWeek currentDayOfWeek&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = (BitwiseDayOfWeek)Math.Pow(2, (double)DateTime.Now.DayOfWeek);&nbsp; &nbsp; &nbsp; &nbsp; // test if today is the scheduled day&nbsp; &nbsp; &nbsp; &nbsp; if ( (currentDayOfWeek & scheduledDayOfWeek) == currentDayOfWeek )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(currentDayOfWeek);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("---------------------");&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }}

森栏

如果您像这样更改枚举:[Flags]public enum DayOfWeek{&nbsp; &nbsp; Sunday = 0,&nbsp; &nbsp; Monday = 1,&nbsp; &nbsp; Tuesday = 2,&nbsp; &nbsp; Wednesday = 3,&nbsp; &nbsp; Thursday = 4,&nbsp; &nbsp; Friday = 5,&nbsp; &nbsp; Saturday = 6}你可以试试这个:class Program{&nbsp; &nbsp; public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int mineInt = (int)mine;&nbsp; &nbsp; &nbsp; &nbsp; int cSharpInt = (int)cSharp;&nbsp; &nbsp; &nbsp; &nbsp; return mineInt == cSharpInt;&nbsp; &nbsp; }&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DateTime dateTime = DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; DayOfWeek dayOfWeek = DayOfWeek.Sunday;&nbsp; &nbsp; &nbsp; &nbsp; bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(areEqual);&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; }}如果你不能改变你的枚举,你可以试试这个:class Program{&nbsp; &nbsp; public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (mine == DayOfWeek.Friday && cSharp == System.DayOfWeek.Friday&nbsp; &nbsp; &nbsp; &nbsp;||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine == DayOfWeek.Monday && cSharp == System.DayOfWeek.Monday&nbsp; &nbsp; &nbsp; &nbsp;||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine == DayOfWeek.Saturday && cSharp == System.DayOfWeek.Saturday&nbsp; &nbsp;||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine == DayOfWeek.Sunday && cSharp == System.DayOfWeek.Sunday&nbsp; &nbsp; &nbsp; &nbsp;||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine == DayOfWeek.Thursday && cSharp == System.DayOfWeek.Thursday&nbsp; &nbsp;||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine == DayOfWeek.Tuesday && cSharp == System.DayOfWeek.Tuesday&nbsp; &nbsp; &nbsp;||&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mine == DayOfWeek.Wednesday && cSharp == System.DayOfWeek.Wednesday)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DateTime dateTime = DateTime.Now;&nbsp; &nbsp; &nbsp; &nbsp; DayOfWeek dayOfWeek = DayOfWeek.Tuesday;&nbsp; &nbsp; &nbsp; &nbsp; bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(areEqual);&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP