(C#)如何将我的枚举转换为字典,将VALUE作为属性,将KEY作为char(枚举的值)
本文关键字:枚举 作为 属性 KEY char VALUE 我的 转换 字典 | 更新日期: 2023-09-27 18:14:01
编码器,
这是我的第一个问题。所以,如果它缺少一些堆叠的模式,请原谅,我会按照你的说法编辑它。
我只需要:将我的枚举转换为一个集合(我开始使用的是字典(,但忽略枚举主值本身。。。我只想要属性(作为XmlEnum(和CHAR值。这样我就可以填充我的下拉列表并感到高兴。
任何代码:
// 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
}
输出:
<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>
很久以前我创建了这个:
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; }
}
您可以使用GetAllValuesFromEnum获取所有属性值,只需更改代码中的属性类型typeof(EnumDescription)
即可