默默的记下枚举的各种用法

工作中经常会遇到使用枚举的地方,当然对于前台页面的枚举使用大多是直接调用就可以了,而设计到后台的管理就经常把枚举当作1个集合来使用了

  List<FineUI.ListItem> albumType = new List<FineUI.ListItem>();

foreach (DemandStatus type in Enum.GetValues(typeof(DemandStatus)))
{
albumType.Add(new FineUI.ListItem(type.ToString(), ((int)type).ToString()));
}
this.ddl_status.DataSource = albumType;
this.ddl_status.DataTextField = "Text";
this.ddl_status.DataValueField = "Value";
this.ddl_status.DataBind();

例如这里绑定枚举到1个DropdownList上就是采用遍历的方式把‘DemandStatus’这个枚举中的值都遍历出来使用

而一般而言就只需要直接使用就可以了

DemandStatus.正常

还有就是在开发当中遇到数据库的值是需要你的枚举来替换的,就需要使用<%# GetDate(Eval("status"))%>

来依次绑定了

protected string GetDate(string status)
{

foreach (ProductStatus item in Enum.GetValues(typeof(ProductStatus)))
{
if (Convert.ToInt32(status) == Convert.ToInt32(item))
{
return item.ToString();
}
}
return string.Empty;
}

 

最近又学到了枚举的公用方法的使用

private static Dictionary<int,string> GetEnumZhDic(Type enumType)

{

string cacheKey=enumType.AssenvktQyakufiedName;

Dictionary<int,string> dic=localCache.GetCache<Dictionary<int,string>>(cacheKey);

if(dic==null)

{

   dic=new Dictionary<int,string>();

   FieldInfo [] fieldInfos=enumType.GetFields(BindingFlags.Public| BindingFlags.DeclaredOnly|BindingFlags.Static);

foreach(FieldInfo info in fieldIndfos)

{

   object [] attrs=info.GetCustomAttributes(typeof(EnumAttribute,false));

  if(attrs==null||attrs.length==0)

{

   continue;

}

string chineseDiscription=((EnumAttribute)attrs[0]).dispay;

int key=(int)Enum.Parse(enumType,info.Name);

dic.Add(key,chineseDiscription);

}

if(dic.Count<=0)

{

throw new Exception("未在属性上配置特性!");

}

localCache.AddCache(cacheKey,dic,5);

}

return SerializeTool.clone<Dictionary<int,string>>(dic);

转载于:https://www.cnblogs.com/chengleijiang/p/4775831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值