对象枚举的实现

来源:互联网 发布:ed视频编辑软件 编辑:程序博客网 时间:2024/06/03 02:43
隐藏行号 复制代码 对象枚举实例代码.
  1. /// 
  2. /// 邮箱状态
  3. /// 
  4. public class EmailStates
  5. {
  6.     static EmailStates()
  7.     {
  8.         Init();
  9.         CheckID();
  10.     }
  11.     private static List<EmailState> _EmailStateList = new List<EmailState>();
  12.     public static List<EmailState> EmailStateList
  13.     {
  14.         get
  15.         {
  16.             return _EmailStateList;
  17.         }
  18.     }
  19.     private static void Init()
  20.     {
  21.         Type type = typeof(EmailStates);
  22.         FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
  23.         foreach (FieldInfo field in fields)
  24.         {
  25.             EmailState es = (EmailState)field.GetValue(null);
  26.             _EmailStateList.Add(es);
  27.         }
  28.     }
  29.     private static void CheckID()
  30.     {
  31.         for (int i = 0; i < _EmailStateList.Count; i++)
  32.         {
  33.             EmailState es = _EmailStateList[i];
  34.             for (int j = i + 1; j < _EmailStateList.Count; j++)
  35.             {
  36.                 if (es.Id == _EmailStateList[j].Id)
  37.                 {
  38.                     throw new Exception("不能有相同的ID");
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     public static EmailState GetEmailStateByName(string name)
  44.     {
  45.         foreach (EmailState es in _EmailStateList)
  46.         {
  47.             if(es.Name==name)
  48.             {
  49.                 return es;
  50.             }
  51.         }
  52.         return null;
  53.     }
  54.     public static EmailState GetEmailStateById(int id)
  55.     {
  56.         foreach (EmailState es in _EmailStateList)
  57.         {
  58.             if (es.Id == id)
  59.             {
  60.                 return es;
  61.             }
  62.         }
  63.         return null;
  64.     }
  65.     public static readonly EmailState 未使用 = new EmailState(1, "未使用");
  66.     public static readonly EmailState 已使用 = new EmailState(2, "已使用");
  67. }
  68. public class EmailState
  69. {
  70.     private int _Id;
  71.     private string _Name;
  72.     public int Id
  73.     {
  74.         get{
  75.             return _Id;
  76.         }
  77.     }
  78.     public string Name
  79.     {
  80.         get{
  81.             return _Name;
  82.         }
  83.     }
  84.     public EmailState(int id,string name)
  85.     {
  86.         _Id = id;
  87.         _Name = name;
  88.     }
  89. }
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

以上是对象枚举的一个实例代码,主要是实现邮箱的状态表示.这样写的好处是可以像枚举一样访问对象,并能很方便的将数据绑定到控件上.还有很多其他好处,自己慢慢体会吧