简体   繁体   English

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

[英]Casting enum value to int when binding to dropdown using reflection

I have some code that binds an enumeration to a dropdown list, however, I'd like build the dropdown to take the integer value from the enum for the value and the description attribute for the text. 我有一些将枚举绑定到下拉列表的代码,但是,我希望构建下拉列表,以从枚举中获取整数值作为文本的值和description属性。

I tried defining the kvPairList as a int/string and casting enumValue and an (int) I also tried converting.toInt32 我尝试将kvPairList定义为int / string并强制转换enumValue和(int)我也尝试过conversion.toInt32

Ideas? 有想法吗?

<select name="DropDownList1" id="DropDownList1"> 
  <option value="1">Option 1</option> 
  <option value="2">Option 2</option> 
  <option value="3">Option 3/option>  
</select

Enum: 枚举:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;

    namespace constants
    {
        public enum Options : int
        {

            [Description("Option 1")]
            Option1 = 1,
            [Description("Option 2")]
            Option2 = 3,
            [Description("Option 3")]
            Option3 = 3
        }
    }

Class: 类:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Reflection;

public class EnumDescription
{
    public static string GetDescription(System.Enum value)
    {
        FieldInfo FieldInfo = value.GetType().GetField(value.ToString());
        DescriptionAttribute[] attributes = (DescriptionAttribute[])FieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if ((attributes.Length > 0))
            return attributes[0].Description;
        else
            return value.ToString();
    }

    public static List<KeyValuePair<string, string>> GetValuesAndDescription(System.Type enumType)
    {
        List<KeyValuePair<string, string>> kvPairList = new List<KeyValuePair<string, string>>();

        foreach (System.Enum enumValue in System.Enum.GetValues(enumType))
        {
            kvPairList.Add(new KeyValuePair<string, string>(enumValue.ToString(), GetDescription(enumValue)));
        }

        return kvPairList;
    }

}

You need to actually cast it to an int before getting the string representation. 在获取字符串表示形式之前,您实际上需要将其强制转换为int。 Otherwise you are getting the representation of the enumeration, rather than the integer. 否则,您将获得枚举的表示形式,而不是整数。

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

Since the value is of type System.Enum and not the underlying enum, a cast won't work. 由于该值的类型为System.Enum而不是基础枚举,所以强制转换将不起作用。 You could otherwise use the appropriate Convert method. 否则,您可以使用适当的Convert方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM