【问题标题】:Cast combobox SelectedValue to Enum type - WinForm将组合框 SelectedValue 转换为 Enum 类型 - WinForm
【发布时间】:2021-11-13 09:14:53
【问题描述】:

嘿家伙,我想将组合框 SelectedValue 转换为 Enum 类型,但选定的值项是一个列表,如 {Title, Value}; Title 是“枚举项的描述属性”Value 是“枚举项”

var EnumItems = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(item =>
                   new
                   {
                       Text = item.GetEnumName(),
                       Value = item
                   }).ToList();
            MyCombo.DataSource = EnumItems;
            MyCombo.DisplayMember = "Text";
            MyCombo.ValueMember = "Value";

GetEnumName() 方法:

public static string GetEnumName(this System.Enum myEnum)
        {
            var enumDisplayName = myEnum.GetType().GetMember(myEnum.ToString()).FirstOrDefault();
            if (enumDisplayName != null)
            {
                //return enumDisplayName.GetCustomAttribute<DescriptionAttribute>()?.Get();
                return string.Format("{0}", (Attribute.GetCustomAttribute(myEnum.GetType().GetField(myEnum.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description, false);
            }

            return "";
        }

我尝试这种方式投射:

var getEnum = (MyEnum)Enum.Parse(typeof(MyEnum), MyCombo.SelectedValue.ToString());

但是这样返回:

System.ArgumentException: '请求的值'{ Text = enumDescriptionText, Value = enumValue }' 未找到。'

你有解决这个问题的办法吗?

【问题讨论】:

    标签: c# enums combobox


    【解决方案1】:

    当您设置数据源时,您会创建匿名类型 'aTextValue 属性:

    comboBox1.DataSource = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().Select(item => new
    {
        Text = item.GetEnumDescription(),
        Value = item
    }).ToList();
    

    所以结果是List&lt;'a&gt;

    当您尝试检索ComboBox 值/项目中的选定项时,它也将是匿名类型 'a 的项目,因此您不能将其显式转换为MyEnum

    解决方案可能正在使用dynamics,它也可以是您的匿名类型,并且在运行时具有TextValue 属性:

    private void OnComboBoxSelectedValueChanged(object sender, EventArgs e)
    {
        dynamic selectedItem = comboBox1.SelectedItem;
    
        if (selectedItem != null)
        {
            MyEnum enumItem = (MyEnum)selectedItem.Value;
            string itemDescription = selectedItem.Text;
    
            _ = MessageBox.Show(string.Format("Enum item: {0}\nItem description: {1}", enumItem, itemDescription));
        }
    }
    

    我有这个结果:

    我的枚举看起来像:

    enum MyEnum
    {
        [Description("My 1st item")]
        MyItem1,
        [Description("My 2nd item")]
        MyItem2,
        [Description("My 3rd item")]
        MyItem3
    }
    

    我的.GetEnumDescription() 被改写为你的.GetEnumName() 三元样式:

    public static class EnumExtensions
    {
        public static string GetEnumDescription(this Enum enumValue) =>
            enumValue.GetType().GetMember(nameof(enumValue)).FirstOrDefault() is MemberInfo
            ? $"{(Attribute.GetCustomAttribute(enumValue.GetType().GetField(nameof(enumValue)), typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description}"
            : string.Empty;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2015-08-04
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多