简体   繁体   English

带有flags属性的枚举

[英]enum with flags attribute

I have the enum: 我有枚举:

    [Flags, Serializable,]
    public enum WeekDays {
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Saturday = 64,
        WeekendDays = Sunday | Saturday,
        WorkDays = Monday | Tuesday | Wednesday | Thursday | Friday,
        EveryDay = WeekendDays | WorkDays
    }

And, I have property WeekDays in a class that contain value of WeekDays enum: 而且,我在一个包含WeekDays枚举值的类中有属性WeekDays

public int WeekDays {get; set;}

For example WorkDays contain 62( from Monday to Friday). 例如, WorkDays包含62(从星期一到星期五)。
How to check that current WeekDays property contain current day? 如何检查当前的WeekDays属性是否包含当天?

Enum具有方法HasFlag用于确定是否在当前实例中设置了一个或多个位字段。

Use the bitwise operator & to see if a value is part of a set: 使用按位运算符&查看某个值是否为集合的一部分:

var today = WeekDays.Thursday;
var workdays = WeekDays.WorkDays;

if((today & workdays) == today) {
    // today is a workday
}

if((today & WeekDays.Friday) == today) {
    // it's friday
}

Use & : 使用&

    [Flags, Serializable,]
    public enum WeekDays
    {
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Saturday = 64,
        WeekendDays = Sunday | Saturday,
        WorkDays = Monday | Tuesday | Wednesday | Thursday | Friday,
        EveryDay = WeekendDays | WorkDays
    }

    public static WeekDays Days { get; set; }

    private static void Main(string[] args)
    {
        WeekDays today = WeekDays.Sunday;

        Days = WeekDays.WorkDays;

        if ((Days & today) == today)
        {
            Console.WriteLine("Today is included in Days");
        }

        Console.ReadKey();
    }
WeekDays weekDayValue = .... ;
var today = Enum.Parse(typeof(WeekDays),DateTime.Now.DayOfWeek.ToString())
bool matches = ( weekDayValue & today ) == today;

do: 做:

int r = (int)(WeekDays.WorkDays & WeekDays.Sunday)
if (r !=0)
  you have it.

You just need to use bitwise boolean logic to do this. 您只需要使用按位布尔逻辑来执行此操作。 if you were to say 如果你要说

WeekDays.Tuesday & WeekDays.WorkDays

then the boolean logic would return 4 (since 4&62 = 4). 那么布尔逻辑将返回4(因为4和62 = 4)。

Essentially the & is saying that for each bit position if both are 1 then return that number. 基本上&表示对于每个位位置,如果两者都是1,则返回该数字。 So you then just need to check if it is > 0. 所以你只需要检查它是否> 0。

To get Today in your current format then you'll need to do a bit of maths... 要以您当前的格式获取今天,那么您需要做一些数学...

(int)DateTime.Now.DayOfWeek will return the day of the week ranging from 0 (sunday) to 6 (saturday). (int)DateTime.Now.DayOfWeek将返回从0(星期日)到6(星期六)的星期几。

We need to map these to match your range. 我们需要映射这些以匹配您的范围。 Fortunately this is easy to do with Math.Pow. 幸运的是,使用Math.Pow很容易。

int today = Math.Pow(2,(int)DateTime.Now.DayOfWeek);
if (today&WeekDays.WorkDays>0)
    isWorkDay = true;
else
    isWorkDay = false;

获取所有枚举名称和值,然后执行“和”计算以测试WeekDays是否包含当天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM