枚举值 `Browsable(false)`

发布于 2024-11-16 13:39:13 字数 222 浏览 1 评论 0原文

有没有办法使枚举值不可浏览到组合框
或者只是不从 Enum.GetValues() 返回?

public enum DomainTypes  
{  
    [Browsable(true)]  
     Client = 1,  
    [Browsable(false)]  
    SecretClient = 2,    
} 

is there a way to make an enum value not browsable to combo box
or just, not to come back from Enum.GetValues() ??

public enum DomainTypes  
{  
    [Browsable(true)]  
     Client = 1,  
    [Browsable(false)]  
    SecretClient = 2,    
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

于我来说 2024-11-23 13:39:13

这是一个通用方法(基于我找不到的另一个答案),您可以在任何枚举上调用它。
顺便说一句,Browsable 属性已在 System.ComponentModel 中定义。
例如:

ComboBox.DataSource = EnumList.Of<DomainTypes>();

...

public class EnumList
{
    public static List<T> Of<T>()
    {
        return Enum.GetValues(typeof(T))
            .Cast<T>()
            .Where(x => 
                {
                    BrowsableAttribute attribute = typeof(T)
                        .GetField(Enum.GetName(typeof(T), x))
                        .GetCustomAttributes(typeof(BrowsableAttribute),false)
                        .FirstOrDefault() as BrowsableAttribute;
                    return attribute == null || attribute.Browsable == true;
                }
            )
        .ToList();
    }
}

This is a generic method (based on another SO answer which I can't find) which you can call on any enum.
By the way, the Browsable attribute is already defined in System.ComponentModel.
For example:

ComboBox.DataSource = EnumList.Of<DomainTypes>();

...

public class EnumList
{
    public static List<T> Of<T>()
    {
        return Enum.GetValues(typeof(T))
            .Cast<T>()
            .Where(x => 
                {
                    BrowsableAttribute attribute = typeof(T)
                        .GetField(Enum.GetName(typeof(T), x))
                        .GetCustomAttributes(typeof(BrowsableAttribute),false)
                        .FirstOrDefault() as BrowsableAttribute;
                    return attribute == null || attribute.Browsable == true;
                }
            )
        .ToList();
    }
}
江心雾 2024-11-23 13:39:13

目前还没有任何方法可以使用 Enum.GetValues() 方法为您执行此操作。如果您想使用属性,您可以创建自己的自定义属性并通过反射使用它:

public class BrowsableAttribute : Attribute
{
  public bool IsBrowsable { get; protected set; }
  public BrowsableAttribute(bool isBrowsable)
  {
    this.IsBrowsable = isBrowsable;
  }
}

public enum DomainTypes  
{  
    [Browsable(true)]  
     Client = 1,  
    [Browsable(false)]  
    SecretClient = 2,    
} 

然后您可以使用反射检查自定义属性并基于 Browsable 属性生成枚举列表。

There is nothing already in place to do this for you with the Enum.GetValues() method. If you want to use attributes, you can create your own custom attribute and use it via reflection:

public class BrowsableAttribute : Attribute
{
  public bool IsBrowsable { get; protected set; }
  public BrowsableAttribute(bool isBrowsable)
  {
    this.IsBrowsable = isBrowsable;
  }
}

public enum DomainTypes  
{  
    [Browsable(true)]  
     Client = 1,  
    [Browsable(false)]  
    SecretClient = 2,    
} 

And then you can use reflection to check for custom attributes and generate a list of Enums based on the Browsable attribute.

辞旧 2024-11-23 13:39:13

这确实无法在 C# 中完成 - 公共枚举公开所有成员。相反,请考虑使用包装类来有选择地隐藏/公开项目。也许是这样的:

public sealed class EnumWrapper
{
    private int _value;
    private string _name;

    private EnumWrapper(int value, string name)
    {
        _value = value;
        _name = name;
    }

    public override string ToString()
    {
        return _name;
    }

    // Allow visibility to only the items you want to
    public static EnumWrapper Client = new EnumWrapper(0, "Client");
    public static EnumWrapper AnotherClient= new EnumWrapper(1, "AnotherClient");

    // The internal keyword makes it only visible internally
    internal static readonly EnumWrapper SecretClient= new EnumWrapper(-1, "SecretClient");
}

希望这有帮助。祝你好运!

It really can't be done in C# - a public enumeration exposes all members. Instead, consider using a wrapper class to hide/expose the items selectively. Maybe something like this:

public sealed class EnumWrapper
{
    private int _value;
    private string _name;

    private EnumWrapper(int value, string name)
    {
        _value = value;
        _name = name;
    }

    public override string ToString()
    {
        return _name;
    }

    // Allow visibility to only the items you want to
    public static EnumWrapper Client = new EnumWrapper(0, "Client");
    public static EnumWrapper AnotherClient= new EnumWrapper(1, "AnotherClient");

    // The internal keyword makes it only visible internally
    internal static readonly EnumWrapper SecretClient= new EnumWrapper(-1, "SecretClient");
}

Hope this helps. Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文