来自长位掩码的枚举值列表

时间:2016-11-16 18:01:40

标签: c# enums bitmask

我们有一个应用程序通过位掩码保护api控制器操作。我想要做的是根据位掩码值获取枚举列表。

以下是Enum的示例:

    [Flags]
    public enum DriverChange : long
    {
        CanAccess = 1 << 0,
        CanView = 1 << 1,
        CanUpdate = 1 << 2,
        MayPerformBaseFunctions = 1 << 3,
        CanAddPool = 1 << 4,
        CanModifyPool = 1 << 5,

    }

我们通过添加位并存储整数值来创建权限。像这样:

public class DriverDataChange
    {
        public const int None = 0;
        public const int View = (int)(Permissions.DriverDataChange.CanAccess |
                                                Permissions.DriverDataChange.CanView |
                                                Permissions.DriverDataChange.MayPerformBaseFunctions);
        public const int MassUpload = (int)(Permissions.DriverDataChange.CanAccess |
                                                Permissions.DriverDataChange.CanView |
                                                Permissions.DriverDataChange.CanUpdate |
                                                Permissions.DriverDataChange.MayPerformBaseFunctions |
                                                Permissions.DriverDataChange.CanPerformMassUpload);
        public const int FullNoMassUpload = (int)(Permissions.DriverDataChange.CanAccess |
                                                Permissions.DriverDataChange.CanView |
                                                Permissions.DriverDataChange.CanUpdate |
                                                Permissions.DriverDataChange.MayPerformBaseFunctions |
                                                Permissions.DriverDataChange.CanAddPool |
                                                Permissions.DriverDataChange.CanModifyPool |
                                                Permissions.DriverDataChange.CanAddDriver |
                                                Permissions.DriverDataChange.CanModifyDriver |
                                                Permissions.DriverDataChange.CanUnassignVehicle |
                                                Permissions.DriverDataChange.CanCloneVehicleDetails |
                                                Permissions.DriverDataChange.CanModifyVehicleClientDataFields);
        public const int Full = (int)(Permissions.DriverDataChange.CanAccess |
                                                Permissions.DriverDataChange.CanView |
                                                Permissions.DriverDataChange.CanUpdate |
                                                Permissions.DriverDataChange.MayPerformBaseFunctions |
                                                Permissions.DriverDataChange.CanAddPool |
                                                Permissions.DriverDataChange.CanModifyPool |
                                                Permissions.DriverDataChange.CanAddDriver |
                                                Permissions.DriverDataChange.CanModifyDriver |
                                                Permissions.DriverDataChange.CanUnassignVehicle |
                                                Permissions.DriverDataChange.CanCloneVehicleDetails |
                                                Permissions.DriverDataChange.CanModifyVehicleClientDataFields |
                                                Permissions.DriverDataChange.CanPerformMassUpload);
    }

我想要做的是根据整数值获取<T>值列表。

我找到this,但它不适用于位移值:

List<Permissions.DriverDataChange> permissionDetails =Enum.GetValues(typeof(Permissions.DriverDataChange))
                                                .Cast<Enum>()
                                                .Where(m => DriverDataChange.Full.HasFlag(m))
                                                .Cast<Permissions.DriverDataChange>();

1 个答案:

答案 0 :(得分:1)

您的问题是,您已将DriverDataChange中的数据定义为int而不是DriverChange

而是像这样定义它们:

public const DriverDataChange View = (DriverDataChange.CanAccess |
                                         DriverDataChange.CanView |
                                         DriverDataChange.MayPerformBaseFunctions);

然后你可以查询它们:

var permissionDetails = Enum.GetValues(typeof(DriverDataChange)).Cast<DriverDataChange>()
                             .Where(m => DriverDataChangeData.View.HasFlag(m));

返回:

  

CanAccess
  CanView
  MayPerformBaseFunctions

编辑:

如果您 DriverDataChange的值保持为int,那么您可以将其投放到支票中:

var permissionDetails = Enum.GetValues(typeof(DriverDataChange)).Cast<DriverDataChange>()
                   .Where(m => ((DriverDataChange)DriverDataChangeData.View).HasFlag(m));