发布于 2011-06-23 13:40:48
参考将Enum转换为字典
public static IDictionary<String, Int32> ConvertEnumToDictionary<K>()
{
if (typeof(K).BaseType != typeof(Enum))
{
throw new InvalidCastException();
}
return Enum.GetValues(typeof(K)).Cast<Int32>().ToDictionary(currentItem => Enum.GetName(typeof(K), currentItem));
}然后,您可以用返回的字典项填充ComboBox。
还请参阅以下内容:
发布于 2011-06-23 13:50:54
我认为最好用一个例子来解释:
说你有一个枚举:
enum MyEnum
{
One,
Two,
Three
}您可以声明如下的方法:
public static void MyEnumMethod(Enum e)
{
var enumValues = Enum.GetValues(e.GetType());
// you can iterate over enumValues with foreach
}你可以这样称呼它:
MyEnumMethod(new MyEnum());发布于 2011-06-23 13:43:51
您可以像这样一般地传递一个枚举:
private void Method(Enum tEnum)
{
Enum.GetValues(tEnum.GetType());
}GetValues会给出这个枚举可能的值。
使用起来会有点奇怪:
Method(EnumType.Value)因此,它可能不像其他想法那么合适。
https://stackoverflow.com/questions/6454950
复制相似问题