简体   繁体   English

(C#)如何将我的Enum转换为以VALUE作为属性,以KEY作为char(枚举的值)的字典?

[英](C#) How can I convert my Enum to a dictionary getting the VALUE as the attribute and the KEY as the char(value of the enum)?

coders, 编码员

It's my first question here. 这是我的第一个问题。 So if it lacks of some stackoverflow pattern, pardon me, i'll edit it as you claim. 因此,如果缺少某些stackoverflow模式,请原谅,我将按照您的要求进行编辑。

All I need: Convert my enums to a collection(the one I've came out to use was the dictionary) but ignoring the enum main value itself... I just want the attribute(as XmlEnum) and the CHAR value . 我需要做的只是:将枚举转换为一个集合(我刚开始使用的是字典),但忽略了枚举主值本身...我只想要属性(如XmlEnum)CHAR值 So I can populate my drop down list and be happy. 因此,我可以填充我的下拉列表并感到高兴。

Whatever code: 任何代码:

// whatever.aspx
<asp:DropDownList ID="ddlGreekTeam" runat="server" ... whatever />
<asp:DropDownList ID="ddlMilitaryTeam" runat="server" ... whatever />

// whatever.aspx.cs
private void OnPageLoad()
{
    PopulateTeamDropDownLists<GreekTeam>(ddlGreekTeam)
    PopulateTeamDropDownLists<MilitaryTeam>(ddlMilitaryTeam)
    // ... whatever
}
private void PopulateTeamDropDownLists<TEnum>(DropDownList ddl)
{
    ddl.Items.Clear();
    ddl.Items.Insert(0, new ListItem("All", null));

    foreach(var team in ListTeams<TEnum>())
        ddl.Items.Add(new ListItem(team.Value.ToString(), team.Key));
}

// method needed
public IDictionary<string, char> ListTeams<TEnum>()
{
    // TODO: the magic I'm looking for...
}

// enums
public enum GreekTeam
{
    [XmlEnum("Alpha Team")]
    Alpha = 'A',
    [XmlEnum("Beta Team")]
    Beta = 'B',
    [XmlEnum("Chi Team")]
    Gamma = 'G',
    // ... whatever
}

public enum MilitaryTeam
{
    [XmlEnum("Alpha Team")]
    Alpha = 'A',
    [XmlEnum("Bravo Team")]
    Bravo = 'B',
    [XmlEnum("Charlie Team")]
    Charlie = 'C',
    // ... whatever
}

OUTPUT: 输出:

<select>
  <option value="A">Alpha Team</option>
  <option value="B">Beta Team</option>
  <option value="G">Gamma Team</option>
  ...
</select>
<select>
  <option value="A">Alpha</option>
  <option value="B">Bravo Team</option>
  <option value="C">Charlie Team</option>
  ...
</select> 

Long time ago I created this: 很久以前,我创建了这个:

public class EnumUtils
{

    public static string GetEnumDescription(object en)
    {
        Type type = en.GetType();
        MemberInfo[] memInfo = type.GetMember(en.ToString());
        if (memInfo != null && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(EnumDescription), false);
            if (attrs != null && attrs.Length > 0)
                return ((EnumDescription)attrs[0]).Text;
        }
        return en.ToString();
    }
    public static List<EnumObject> GetAllValuesFromEnum<T>()
    {
        List<EnumObject> AllValue = new List<EnumObject>();
        foreach (Enum enm in Enum.GetValues(typeof(T)))
        {
            AllValue.Add(new EnumObject(GetEnumDescription(enm), enm));
        }
        return AllValue;

    }
    public static Enum GetEnumValue<T>(string sEnumValue)
    {
        //List<EnumObject> allobject = GetAllValuesFromEnum<T>();
        foreach (Enum enm in Enum.GetValues(typeof(T)))
        {
            if (enm.ToString() == sEnumValue)
                return enm;
        }
        return null;
    }
    public static Object GetEnumValueFromDescription<T>(string sEnumDescription) //where T: Enum 
    {
        List<EnumObject> allobject = GetAllValuesFromEnum<T>();
        foreach (EnumObject item in allobject)
        {
            if (item.EnumDisplay == sEnumDescription)
                return item.EnumValue;
        }
        return default(T) as Enum;
    }

}

public class EnumObject
{
    private string _EnumDisplay;
    private Enum _EnumValue;

    public EnumObject(string enumDisplay, Enum enumValue)
    {
        _EnumDisplay = enumDisplay;
        _EnumValue = enumValue;
    }
    public string EnumDisplay
    {
        get
        {
            return _EnumDisplay;
        }
        set
        {
            _EnumDisplay = value;
        }
    }

    public Enum EnumValue
    {
        get { return _EnumValue; }
        set { _EnumValue = value; }
    }
}
public class EnumDescription : Attribute
{
    public string Text;
    public EnumDescription(string text)
    { Text = text; }
}

You can use GetAllValuesFromEnum to get all attribute values, just change the attribute type typeof(EnumDescription) in the code 您可以使用GetAllValuesFromEnum获取所有属性值,只需在代码中更改属性类型typeof(EnumDescription)

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

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