有没有办法让枚举值不能浏览组合框
或只是,不要回来Enum.GetValues()?
public enum DomainTypes
{
[Browsable(true)]
Client = 1,
[Browsable(false)]
SecretClient = 2,
}
Run Code Online (Sandbox Code Playgroud)
这是一个通用方法(基于另一个我无法找到的SO答案),您可以在任何枚举上调用它.顺便说一下,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();
}
}
Run Code Online (Sandbox Code Playgroud)