簡體   English   中英

刪除權限枚舉標記在C#中正確的方式

[英]Remove privilege enum flags the right way in C#

我有一個用戶權限的枚舉類型,如下所示:

[Flags]
public enum UserPrivileges : byte
{
    None = 0,                                     // 0000 0000
    View = 1 << 0,                                // 0000 0001
    Import = 1 << 1,                              // 0000 0010
    Export = 1 << 2,                              // 0000 0100
    Supervisor = View | Import | Export | 1 << 3, // 0000 1111
    Admin = Supervisor | 1 << 4                   // 0001 1111
}

這些值通過值轉換器綁定到GUI中的CheckBoxes。 (我希望這樣做盡可能通用,因為還有不同的權限[例如EmployeePrivileges])

public class ByteFlagsEnumValueConverter : IValueConverter
{
    private byte _targetValue;

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        var mask = (byte)parameter;
        _targetValue = (byte)value;
        return ((mask | _targetValue) == _targetValue);
    }

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        var mask = (byte)parameter;

        if ((bool)value)
        {
            _targetValue |= mask;
        }
        else
        {
            // Get next superflag for mask (e.g. 0110 -> 1111)
            var b = mask;
            b--;
            b |= (byte)(b >> 1);
            b |= (byte)(b >> 2);
            b |= (byte)(b >> 4);
            b |= (byte)(b >> 8);

            // if you remove a superflag (e.g. 1111) also remove
            // everything higher than this flag
            if (mask == b || mask == 1)
                _targetValue &= (byte)(mask >> 1);
            else
                // ????????
        }

        return Enum.Parse(targetType, _targetValue.ToString());
    }
}

這對於在GUI中向用戶顯示和添加權限非常有用。 它也適用於刪除超級標志,如Supervisor(所有標志>= Supervisor被刪除,其他標志不會更改)。

問題是當我取消選中導入時,例如,我想刪除所有Superflags(Supervisor,Admin)但是想保留其他標志(View,Export)。

0001 1111 // Admin
0000 0010 // Import
---------
0000 0101 // View | Export

但我還沒有想出如何實現這一目標。 Anyboy誰有一個很好的解決方案?

如果我明白你想要什么,這應該可以勝任

    byte tmp = 1 << 3 | 1 << 4;
    byte removeSuperFlagMask = (byte) (~tmp);

    byte notSuperflagsMask = 1 << 3 | 1 << 2 | 1 << 1;
    if ((valueToRemove & notSuperflagsMask) != 0)
    {
        newValue = (byte)(removeSuperFlagMask & currentValue & ~valueToRemove);
    }

如果我理解正確你想刪除SupervisorAdmin

UserPrivileges newPrivileges = (UserPrivileges)(((byte)currentPrivileges) & 7;

它會像這樣執行:

0001 1111 // Admin
0000 0111 // 7 flag
---------
0000 0111 // Remain only where 7 bit was set.

另一個例子

0000 0011 // View | Import
0000 0111 // 7 flag
---------
0000 0011 // Remain only where 7 bit was set.

保留我的意思是,如果設置7標志,它將保持結果中的值(beein 01 )。 如果7標志為0 ,它將把值殺死為0

Mfz給我帶來了正確的方式但是,它對我來說不夠通用,所以我提出了另一個解決方案:

    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        var mask = (byte)parameter;

        if ((bool)value)
        {
            _targetValue |= mask;
        }
        else
        {
            if (IsSuperFlag(mask) && mask != 1)
                _targetValue &= (byte)(mask >> 1);
            else
            {
                // Get all flags from enum type that are no SuperFlags
                var flags = Enum.GetValues(targetType).Cast<Enum>();
                flags = flags.Where(x => !IsSuperFlag(System.Convert.ToByte(x)));

                long nonSuperFlags = 0;

                foreach (var flag in flags)
                {
                    nonSuperFlags |= System.Convert.ToByte(flag);
                }

                // Now only remove everything except the nonSuperFlags
                // and then remove the mask
                _targetValue &= (byte)(~(_targetValue ^ nonSuperFlags | mask));

            }
        }

        return Enum.Parse(targetType, _targetValue.ToString());
    }

    private bool IsSuperFlag(byte flag)
    {
        var b = flag;
        b--;
        b |= (byte)(b >> 1);
        b |= (byte)(b >> 2);
        b |= (byte)(b >> 4);
        b |= (byte)(b >> 8);

        return b == flag;
    }

我只是為枚舉類型獲得了所有非超級標志,並且它們只刪除了超級標志,然后刪除了標志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM