0

我有一个命名空间,我在其中声明了一个枚举,如下所示:

namespace IXMSoft.Business.SDK.Data
{
using System;

public enum BaudRate
{
    BR115200 = 7,
    BR19200 = 4,
    BR230400 = 8,
    BR2400 = 1,
    BR38400 = 5,
    BR4800 = 2,
    BR57600 = 6,
    BR9600 = 3
  }
}

当我在另一个命名空间中的组合框中检索这些值时,使用语句

comboBox1.Items.Add(BaudRate.BR5700);

它显示的值例如

“BR5700”

我想remove BR in front and just want to display the value as "5700"。我应该怎么办?

4

5 回答 5

4

使用DescriptionAttribute适当的扩展方法将其读出。

public enum BaudRate
{
    [Description("115200 kb")]
    BR115200 = 7,
    [Description("19200 kb")]
    BR19200 = 4,
    [Description("230400 kb")]
    BR230400 = 8,
    [Description("2400 kb")]
    BR2400 = 1,
    [Description("115200 kb")]
    BR38400 = 5,
    [Description("4800 kb")]
    BR4800 = 2,
    [Description("57600 kb")]
    BR57600 = 6,
    [Description("9600 kb")]
    BR9600 = 3
}

扩展方法:

public static class EnumExtension
{
    /// <summary>
    /// Gets the string of an DescriptionAttribute of an Enum.
    /// </summary>
    /// <param name="value">The Enum value for which the description is needed.</param>
    /// <returns>If a DescriptionAttribute is set it return the content of it.
    /// Otherwise just the raw name as string.</returns>
    public static string Description(this Enum value)
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }

        string description = value.ToString();
        FieldInfo fieldInfo = value.GetType().GetField(description);
        DescriptionAttribute[] attributes =
           (DescriptionAttribute[])
         fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attributes != null && attributes.Length > 0)
        {
            description = attributes[0].Description;
        }

        return description;
    }

    /// <summary>
    /// Creates an List with all keys and values of a given Enum class
    /// </summary>
    /// <typeparam name="T">Must be derived from class Enum!</typeparam>
    /// <returns>A list of KeyValuePair&lt;Enum, string&gt; with all available
    /// names and values of the given Enum.</returns>
    public static IList<KeyValuePair<Enum, string>> ToList<T>() where T : struct
    {
        var type = typeof(T);

        if (!type.IsEnum)
        {
            throw new ArgumentException("T must be an enum");
        }

        return (IList<KeyValuePair<Enum, string>>)
                Enum.GetValues(type)
                    .OfType<Enum>()
                    .Select(e => new KeyValuePair<Enum, string>(e, e.Description()))
                    .ToArray();
    }

    public static T GetValueFromDescription<T>(string description) where T : struct
    {
        var type = typeof(T);

        if(!type.IsEnum)
        {
            throw new ArgumentException("T must be an enum");
        }

        foreach(var field in type.GetFields())
        {
            var attribute = Attribute.GetCustomAttribute(field,
                typeof(DescriptionAttribute)) as DescriptionAttribute;

            if(attribute != null)
            {
                if(attribute.Description == description)
                {
                    return (T)field.GetValue(null);
                }
            }
            else
            {
                if(field.Name == description)
                {
                    return (T)field.GetValue(null);
                }
            }
        }

        throw new ArgumentOutOfRangeException("description");
        // or return default(T);
    }
}

在和您可以通过调用简单地将其应用于您的组合框:

var list = EnumExtension.ToList<BaudRate>();
myComboBox.DataSource = list;
myComboBox.ValueMember = "Key";
myComboBox.DisplayMember = "Value";
于 2013-05-27T07:08:49.247 回答
2

string.replace 示例:

BaudRate.BR115200.ToString().Replace("BR","");

带有子字符串的示例:

BaudRate.BR115200.ToString().Substring(2);
于 2013-05-27T07:05:12.093 回答
2

从枚举名称中删除 BR 似乎是最合乎逻辑的做法。鉴于您的枚举本身已命名BaudRate,BR 是多余的。并且鉴于它存在于每个值上,它不会为值名称增加任何描述能力。并且鉴于枚举值始终以枚举名称为前缀,结果将始终是明确的(BaudRate.9600而不是BaudRate.BR9600)。

如果您不能/不想这样做,那么您需要BaudRate.XXX.ToString().Substring(2)在添加之前对每个值运行 a 以删除前两个字符。

于 2013-05-27T07:06:26.580 回答
1
public enum BaudRate
{
    BR115200 = 7,
    BR19200 = 4,
    BR230400 = 8,
    BR2400 = 1,
    BR38400 = 5,
    BR4800 = 2,
    BR57600 = 6,
    BR9600 = 3
  }
}

foreach (string name in Enum.GetNames(BaudRate))
{
    cmbEnum.Items.Add(name.Replace("BR",""));
}
于 2013-05-27T07:08:10.560 回答
0

将您的组合框定义如下所示:

<combobox>
   <ComboBoxItem>--</ComboBoxItem>
   <ComboBoxItem>2400</ComboBoxItem>
   <ComboBoxItem>4800</ComboBoxItem>
   <ComboBoxItem>9600</ComboBoxItem>
   <ComboBoxItem>19200</ComboBoxItem> // and soo on
</combobox>

并将您的枚举绑定到组合框

于 2013-05-27T07:23:48.417 回答