长位掩码的枚举值列表
本文关键字:列表 枚举 掩码 | 更新日期: 2023-09-27 18:18:09
我们有一个通过位掩码保护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>
值的列表。
我发现了这个,但它不适用于位移位值:
List<Permissions.DriverDataChange> permissionDetails =Enum.GetValues(typeof(Permissions.DriverDataChange))
.Cast<Enum>()
.Where(m => DriverDataChange.Full.HasFlag(m))
.Cast<Permissions.DriverDataChange>();
您的问题是您已将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));
我是这样做的
var permissionDetails =
Enum.GetValues(typeof(DriverDataChange))
.Cast<int>()
.Where(m => (DriverDataChangeData.View & m) > 0)
.Cast<DriverDataChange>();
我还写了一个扩展
public static class EnumExtensions
{
public static IEnumerable<T> UnfoldBitmask<T>(int bitMask)
{
return Enum.GetValues(typeof(T)).Cast<int>().Where(m => (bitMask & m) > 0).Cast<T>();
}
}
你可以这样调用
int permissionDetails = EnumExtensions.UnfoldBitmask<DriverDataChange>(DriverDataChangeData.View );