枚举值 `Browsable(false)`
有没有办法使枚举值不可浏览到组合框
或者只是不从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个通用方法(基于我找不到的另一个答案),您可以在任何枚举上调用它。
顺便说一句,Browsable 属性已在 System.ComponentModel 中定义。
例如:
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:
目前还没有任何方法可以使用
Enum.GetValues()
方法为您执行此操作。如果您想使用属性,您可以创建自己的自定义属性并通过反射使用它:然后您可以使用反射检查自定义属性并基于
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:And then you can use reflection to check for custom attributes and generate a list of Enums based on the
Browsable
attribute.这确实无法在 C# 中完成 - 公共枚举公开所有成员。相反,请考虑使用包装类来有选择地隐藏/公开项目。也许是这样的:
希望这有帮助。祝你好运!
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:
Hope this helps. Good luck!