关于枚举的遍历问题
现在有个枚举
public enum days
{
sunday=0,
monday=1,
}
Type t= typeof(days);
string[] a= t.GetEnumNames();
--定义一个枚举类型
public enum Gender
{
[Description("男")]
Male = 1,
[Description("女")]
Female = 2
}
--定义一个枚举类
public static class EnumDataUtility
{
private static Dictionary<string, object> _EnumItemCollectionsStock;
static EnumDataUtility()
{
_EnumItemCollectionsStock = new Dictionary<string, object>();
}
/// <summary>
/// 获取一个枚举集合类。
/// </summary>
/// <typeparam name="TEnum"></typeparam>
/// <returns></returns>
public static EnumItemCollection<TEnum> GetEnumItemCollection<TEnum>() where TEnum : struct
{
string enumKey = typeof(TEnum).FullName;
if (!_EnumItemCollectionsStock.ContainsKey(enumKey)
[解决办法]
_EnumItemCollectionsStock[enumKey] == null)
{
lock (_EnumItemCollectionsStock)
{
EnumItemCollection<TEnum> collection = new EnumItemCollection<TEnum>();
_EnumItemCollectionsStock[enumKey] = collection;
}
}
return _EnumItemCollectionsStock[enumKey] as EnumItemCollection<TEnum>;
}
}
//绑定DropDownList控件
this.ddl_Gender.DataSource = EnumDataUtility.GetEnumItemCollection<Gender>().ToList().Select(t => { return new { Text = t.Description, Value = t.Value }; });
this.ddl_Gender.DataTextField = "Text";
this.ddl_Gender.DataValueField = "Value";
this.ddl_Gender.DataBind();
this.ddl_Gender.Items.Insert(0, new ListItem("<-- 性別 -->", ""));