相关疑难解决方法(0)

位掩码(标志)枚举变得太大时该怎么办

我在我的应用程序中拥有一组非常大的权限,我使用Flags枚举表示.它正在快速接近长数据类型的实际上限.我不得不提出一个战略,很快就会转变为一个不同的结构.现在,我可以将此列表分解为更小的部分,但是,根据我们的应用程序布局,这已经只是我们应用程序的总体权限的一部分.在管理权限时,我们广泛使用这种区别用于显示目的,如果我可以避免,我宁愿不再重新访问该代码.

还有其他人遇到过这个问题吗?你是怎么过去的?一般的例子很好,但我最感兴趣的是ac#specific example,如果有任何语言特定的技巧可以用来完成工作.

可能不是必需的,但这里是当前为我正在处理的应用程序部分定义的权限列表.

//Subgroup WebAgent
[Flags]
public enum WebAgentPermission : long
{
    [DescriptionAttribute("View Rule Group")]
    ViewRuleGroup = 1,
    [DescriptionAttribute("Add Rule Group")]
    AddRuleGroup = 2,
    [DescriptionAttribute("Edit Rule Group")]
    EditRuleGroup = 4,
    [DescriptionAttribute("Delete Rule Group")]
    DeleteRuleGroup = 8,
    [DescriptionAttribute("View Rule")]
    ViewRule = 16,
    [DescriptionAttribute("Add Rule")]
    AddRule = 32,
    [DescriptionAttribute("Edit Rule")]
    EditRule = 64,
    [DescriptionAttribute("Delete Rule")]
    DeleteRule = 128,
    [DescriptionAttribute("View Location")]
    ViewLocation = 256,
    [DescriptionAttribute("Add Location")]
    AddLocation = 512,
    [DescriptionAttribute("Edit Location")]
    EditLocation = 1024,
    [DescriptionAttribute("Delete Location")]
    DeleteLocation = 2048,
    [DescriptionAttribute("View Volume Statistics")] …
Run Code Online (Sandbox Code Playgroud)

c# permissions enums types bitmask

70
推荐指数
6
解决办法
4万
查看次数

如何通过反射在单个调用中获取字段和属性?

如果在某处覆盖,我道歉.我在发帖前做过研究!

好吧,所以问题...我正在使用GetType().GetProperties,但它没有返回简单的实例字段,它们没有get/set ...所以我使用了.GetFields,找到它们,但我希望得到一个简单的单个对象来获取/设置一个值而不在字段和属性之间翻转......这可能吗?

我目前的代码适用于PropertyInfo,它工作得很好,但那不是我认为的字段吗?

[编辑] 这是我提出的解决方案,这是很好的.感谢大家....

    // some logic borrowed from James Newton-King, http://www.newtonsoft.com
    public static void SetValue(this MemberInfo member, object property, object value)
    {
        if (member.MemberType == MemberTypes.Property)
            ((PropertyInfo)member).SetValue(property, value, null);
        else if (member.MemberType == MemberTypes.Field)
            ((FieldInfo)member).SetValue(property, value);
        else
            throw new Exception("Property must be of type FieldInfo or PropertyInfo");
    }

    public static object GetValue(this MemberInfo member, object property)
    {
        if (member.MemberType == MemberTypes.Property)
            return ((PropertyInfo)member).GetValue(property, null);
        else if (member.MemberType == MemberTypes.Field)
            return ((FieldInfo)member).GetValue(property);
        else
            throw new Exception("Property must be …
Run Code Online (Sandbox Code Playgroud)

c# reflection

23
推荐指数
4
解决办法
2万
查看次数

标签 统计

c# ×2

bitmask ×1

enums ×1

permissions ×1

reflection ×1

types ×1