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

How to compare standard DayOfWeek to own with Flag

我有自己的 DaysOfWeek Flag 枚举(有点像 https://docs.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 与我的进行比较。我该怎么做?

由于您的枚举使用与 built-in DayOfWeek 相同的天数顺序,您需要做的就是使用 DayOfWeek 类型的变量作为 [=] 的指数13=] 然后 bitwise-AND 它与您的枚举变量。

类似这样的事情(这将检查您的枚举的 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 是 built-in .NET 类型。您可以在一周中的任何一天做同样的事情。

编辑

正如@HenkHolterman 所指出的(感谢),您将 Sunday 设置为 0 会遇到问题。 Flags 枚举通常应以名为 None 的成员开头,该成员等于 0 并且表示设置了变量标志的 none。

感谢大家的帮助。 终于有办法了

拥有带有标志的 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;

操场:https://dotnetfiddle.net/sV3yge

如果您像这样更改枚举:

[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();
    }
}
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();
    }
}