一个关于复合型枚举标志位的功能类

我们经常需要设置这样一些标志位:比如对于模块我们分浏览、审核、修改、删除、冻结等不同的

权限,可能还有无权限和完全权限,这些权限可叠加,可能还要分级别。对于每个模块我们都要设

置一个用户对此模块的相关权限,所以就不可避免地要对这些标志位进行绑定、显示、修改等。下

面这个类就以颜色为例子提供了相应的操作,大家有好的想法可以一起来探讨。
-----------------------
public class ColorType
 {
      //枚举所有标志,最小项应该为0,
      //最大项为前面数值的总和,这样最大项就包括前面所有的组合
      [Flags]  
      public enum enumColorType
      {
       Transparent=0,
       Yellow = 1,
       Red = 2,
       Blue = 4,
       AllColor=7
      }
  public const string TRANSPARENT = "透明";
  public const string YELLOW = "黄色";
  public const string RED = "红色";
  public const string BLUE = "蓝色";
  public const string ALLCOLOR = "全色";

  private static System.Collections.Specialized.ListDictionary _lstDicItem;
  
  //显示数据和单选情况下所用的数据源
  public static IDictionary dicSingleOrViewColor
  {
   get
   {
    if (_lstDicItem == null)
    {
     //由于枚举的项目较少,所以使用了ListDictionary;
     //如果项目较多且需要排序,可以使用SortedList
     _lstDicItem = new System.Collections.Specialized.ListDictionary();

     foreach (Int32 nConstValue in System.Enum.GetValues(typeof(enumColorType)))
     {
      //使用String类型做为KEY可减少拆装箱
      _lstDicItem.Add(nConstValue.ToString(), typeof(ColorType).GetField(Enum.ToObject(typeof(enumColorType),nConstValue).ToString().ToUpper()).GetValue(null));
     }
    }
    return _lstDicItem;
   }
  }
  //复选情况下所用的数据源,您也可以声明另一个静态变量来存储它
  public static IDictionary dicMultipleColor
  {
   get
   {
    //不包含第一和最后一项。比如CheckBoxList,用户不选就是第一项,全选就是最后一项
    System.Collections.Specialized.ListDictionary _lstDicItemCopied = new System.Collections.Specialized.ListDictionary();
    foreach (DictionaryEntry DicEntry in _lstDicItem)
    {
     if (DicEntry.Key.ToString() == enumColorType.AllColor.ToString() || DicEntry.Key.ToString() == enumColorType.Transparent.ToString()) continue;
     _lstDicItemCopied.Add(DicEntry.Key,DicEntry.Value);
    }
    return _lstDicItemCopied;
   }
  }
  //把Key或者Value解析成相应的值
  public static Int32 ParseValue(string strKeyOrValue)
  {
   int nSumTotal = 0;
   if(Char.IsDigit(strKeyOrValue[0]))
   {
    if (strKeyOrValue.IndexOf(',') == -1) return Int32.Parse(strKeyOrValue);
    foreach (string strOneValue in strKeyOrValue.Split(','))
    {
     nSumTotal += Int32.Parse(strOneValue);
    }    
   }
   else
   {
    nSumTotal = Convert.ToInt32((enumColorType)(System.Enum.Parse(typeof(enumColorType), strKeyOrValue,true)));
   }
   
   return nSumTotal;
  }
  //把Key或者Value解析成相应的汉字说明
  public static string ParseValueWord(string strKeyOrValue,bool boolParsed)
  {        
   if (strKeyOrValue.IndexOf(",") > -1)
   {
    if (!boolParsed)
    {
     int nParsedValue = ParseValue(strKeyOrValue);
     if (dicSingleOrViewColor.Contains(nParsedValue.ToString()))
     {
      return dicSingleOrViewColor[nParsedValue.ToString()].ToString();
     }
    }
    string strComboWords = "";
    string[] strSepWords = strKeyOrValue.Split(',');
    foreach (string strOneWord in strSepWords)
    {
     if (strComboWords.Length > 0)
     {
      strComboWords += "&";
     }
     if (Char.IsDigit(strOneWord[0]))
     {
      strComboWords += dicSingleOrViewColor[strOneWord].ToString();
     }    
     else
     {
      object obj = System.Enum.Parse(typeof(enumColorType), strOneWord,true);
      strComboWords += dicSingleOrViewColor[((Int32)((enumColorType)obj)).ToString()].ToString();     
     }
    }     
    return strComboWords;
   }
   else
   {
    enumColorType enumParsedColor =(enumColorType)(System.Enum.Parse(typeof(enumColorType), strKeyOrValue,true));
    
    if (enumParsedColor.ToString().IndexOf(",") > -1)
    {
     return ParseValueWord(enumParsedColor.ToString(),true);
    }
    else
    {
     return dicSingleOrViewColor[((Int32)enumParsedColor).ToString()].ToString();
    }
   }    
  }
  public static string ParseValueWord(string strKeyOrValue)
  {
   return ParseValueWord(strKeyOrValue,false);
  }
  //判断总值中是否包括某一个值,也可循环调用此函数对一组CheckBox打勾
  public static bool isContained(Int32 nTotalValue,Int32 nOneColor)
  {
   if(nTotalValue == nOneColor) return true;
   if(nTotalValue < nOneColor) return false;
   //什么值都包括0,所以要特殊处理
   if(nOneColor == 0) return false;   
   return ((nTotalValue & nOneColor) == nOneColor);
  }
  public static bool isContained(Int32 nTotalValue,enumColorType oneColor)
  {
   return (isContained(nTotalValue,(Int32)oneColor));
  }
  //找到值最大的枚举项
  public static enumColorType GetMaxItem(Int32 nTotalValue)
  {
   Int32 nMax = 0;
   //未使用SortedList,所以无法按从大到小的顺序找
   foreach (Int32 nConstValue in System.Enum.GetValues(typeof(enumColorType)))
   {
    if (isContained(nTotalValue,nConstValue) && nConstValue > nMax ) nMax = nConstValue;
   }
   return (enumColorType)nMax;
     }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值