C#Enum Flag - 两种绑定方式 - 枚举 - 类-enum

时间:2015-07-16 21:18:40

标签: c# enums enum-flags

我有以下枚举

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

在用户界面中,用户可以选择ceratain天:例如星期一,星期二,星期三。用户选择星期一,星期二,星期三是7.这个值保存在数据库中名为Days的列中。

现在,如果我有一个班级:

public class Week
{
    public bool Monday { get; set; }
    public bool Tuesday { get; set; }
    public bool Wednesday { get; set; }
    public bool Thursday { get; set; }
    public bool Friday { get; set; }
    public bool Saturday { get; set; }
    public bool Sunday { get; set; }
}

如何绑定该值7并使相应的属性为true或false。示例:7相当于周一,周二,周三枚举。如果我将值7转换为我的星期周,结果将是属性:星期一,星期二,星期三是真,其余是假。

如果我有一个课程周,其中属性:星期一,星期二,星期三是真的,并将其转换为枚举周末,结果将是7。

我该怎么做?

3 个答案:

答案 0 :(得分:2)

最简单的方法之一是使用按位和(&)或使用Enum.HasFlags()来检查标记。我将展示两者混合:

Week Bind(WeekDays days)
{
    var w = new Week();
    w.Monday = (days & WeekDays.Monday) == WeekDays.Monday;
    w.Tuesday = days.HasFlag(WeekDays.Tuesday);
    w.Wednesday = (days & WeekDays.Wednesday) == WeekDays.Wednesday;
    w.Thursday = days.HasFlag(WeekDays.Thursday);
    w.Friday = (days & WeekDays.Friday) == WeekDays.Friday;
    w.Saturday = days.HasFlag(WeekDays.Saturday);
    w.Sunday = days.HasFlag(WeekDays.Sunday);

    return w;
}

答案 1 :(得分:1)

您可以使Week具有类型WeekDays的属性或字段,以跟踪哪些标志处于活动状态。然后所有的布尔属性都只是检查枚举值,并在set上正确更新它。这允许您这样做:

Week w = new Week();
w.Monday = true;
Console.WriteLine(w.Days); // Monday
w.Tuesday = true;
w.Wednesday = true;
Console.WriteLine(w.Days); // Monday, Tuesday, Wednesday

请参阅下面的Week代码,该代码非常详细(尽管引入了SetDaysFlag辅助方法):

public class Week
{
    public WeekDays Days
    { get; set; }

    public bool Monday
    {
        get { return (Days & WeekDays.Monday) != 0; }
        set { SetDaysFlag(WeekDays.Monday, value); }
    }

    public bool Tuesday
    {
        get { return (Days & WeekDays.Tuesday) != 0; }
        set { SetDaysFlag(WeekDays.Tuesday, value); }
    }

    public bool Wednesday
    {
        get { return (Days & WeekDays.Wednesday) != 0; }
        set { SetDaysFlag(WeekDays.Wednesday, value); }
    }

    public bool Thursday
    {
        get { return (Days & WeekDays.Thursday) != 0; }
        set { SetDaysFlag(WeekDays.Thursday, value); }
    }

    public bool Friday
    {
        get { return (Days & WeekDays.Friday) != 0; }
        set { SetDaysFlag(WeekDays.Friday, value); }
    }

    public bool Saturday
    {
        get { return (Days & WeekDays.Saturday) != 0; }
        set { SetDaysFlag(WeekDays.Saturday, value); }
    }

    public bool Sunday
    {
        get { return (Days & WeekDays.Sunday) != 0; }
        set { SetDaysFlag(WeekDays.Sunday, value); }
    }

    /// <summary>
    /// Set or unset the flag on the <c>Days</c> property.
    /// </summary>
    /// <param name="flag">The flag to set or unset.</param>
    /// <param name="state">True when the flag should be set, or false when it should be removed.</param>
    private void SetDaysFlag (WeekDays flag, bool state)
    {
        if (state)
            Days |= flag;
        else
            Days &= ~flag;
    }
}

答案 2 :(得分:0)

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

public class Week
{

    public bool Monday { get; set; }
    public bool Tuesday { get; set; }
    public bool Wednesday { get; set; }
    public bool Thursday { get; set; }
    public bool Friday { get; set; }
    public bool Saturday { get; set; }
    public bool Sunday { get; set; }

    public static explicit operator WeekDays(Week week)
    {

        var points = new[]
        {
            week.Monday,
            week.Tuesday,
            week.Wednesday,
            week.Thursday,
            week.Friday,
            week.Saturday,
            week.Sunday
        };

        WeekDays weekDays = 0;

        for (var i = 0; i < points.Length; i++)
        {
            if (points[i])
            {
                weekDays = weekDays | (WeekDays)(1 << i);
            }
        }

        return weekDays;

    }

    public static explicit operator Week(WeekDays weekDays)
    {
        return new Week
        {
            Monday = weekDays.HasFlag(WeekDays.Monday),
            Tuesday = weekDays.HasFlag(WeekDays.Tuesday),
            Wednesday = weekDays.HasFlag(WeekDays.Wednesday),
            Thursday = weekDays.HasFlag(WeekDays.Thursday),
            Friday = weekDays.HasFlag(WeekDays.Friday),
            Saturday = weekDays.HasFlag(WeekDays.Saturday),
            Sunday = weekDays.HasFlag(WeekDays.Sunday)
        };
    }

    public override string ToString()
    {
        return String.Format("{0},{1},{2},{3},{4},{5},{6}", Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
    }
}