且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用反射绑定到下拉列表时将枚举值强制转换为int

更新时间:2023-02-04 07:59:06

在获取字符串表示之前,您需要将它强制转换为int。否则,您将获得枚举的表示,而不是整数。

You need to actually cast it to an int before getting the string representation. Otherwise you are getting the representation of the enumeration, rather than the integer.

kvPairList.Add(new KeyValuePair<string, string>(((int)enumValue).ToString(), GetDescription(enumValue)));






由于值为 System.Enum 而不是底层枚举,转换将不工作。否则可以使用适当的 Convert 方法。


Since the value is of type System.Enum and not the underlying enum, a cast won't work. You could otherwise use the appropriate Convert method.

kvPairList.Add(new KeyValuePair<string, string>(Convert.ToInt32(enumValue).ToString(), GetDescription(enumValue)));