zoukankan      html  css  js  c++  java
  • 默默的记下枚举的各种用法

    工作中经常会遇到使用枚举的地方,当然对于前台页面的枚举使用大多是直接调用就可以了,而设计到后台的管理就经常把枚举当作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);

  • 相关阅读:
    团队项目冲刺第十天
    gradle文件配置
    idea无Android项目
    php第二次实验报告
    最长回文字串(hdu 3068)
    优先队列实现哈弗曼最小权值
    最小生成树 克鲁斯卡尔(Kruskal)算法求最小生成树
    背包问题------ 分类: ACM 2015-08-03 20:57 1人阅读 评论(0
    Cent Savings (DP) 分类: ACM dp 2015-08-0
    Judging Troubles (multiset查找) 分类: ACM STL
  • 原文地址:https://www.cnblogs.com/chengleijiang/p/4775831.html
Copyright © 2011-2022 走看看