C# Enum Flag - 两种绑定方式 - enum - class -enum

C# Enum Flag - two ways binding - enum - class -enum

我有以下枚举

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

在UI中,用户可以select特定的日子:例如星期一、星期二、星期三。星期一、星期二、星期三的用户 selection 是 7。此值保存在数据库中名为 Days 的列中。

现在如果我有 class:

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 并使适当的属性为真或假。示例:7 等同于星期一、星期二、星期三枚举。如果我将值 7 转换为我的 class 周,结果将是属性:星期一、星期二、星期三为真,其余为假。

如果我有一个 class 周,其中属性:星期一、星期二、星期三为真,并将其转换为枚举 WeekDays,结果将为 7。

我该怎么做?

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

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;
    }
}

最简单的方法之一是使用按位与 (&) 或使用 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;
}
[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);
    }
}