對象枚舉的實現

隱藏行號 複製代碼 對象枚舉實例代碼.
  1. 
    
  2. /// 
    
  3. /// 郵箱狀態
    
  4. /// 
    
  5. public class EmailStates
    
  6. {
    
  7.     static EmailStates()
    
  8.     {
    
  9.         Init();
    
  10.         CheckID();
    
  11.     }
    
  12.     private static List<EmailState> _EmailStateList = new List<EmailState>();
    
  13.     public static List<EmailState> EmailStateList
    
  14.     {
    
  15.         get
    
  16.         {
    
  17.             return _EmailStateList;
    
  18.         }
    
  19.     }
    
  20. 
    
  21.     private static void Init()
    
  22.     {
    
  23.         Type type = typeof(EmailStates);
    
  24.         FieldInfo[] fields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
    
  25.         foreach (FieldInfo field in fields)
    
  26.         {
    
  27.             EmailState es = (EmailState)field.GetValue(null);
    
  28.             _EmailStateList.Add(es);
    
  29.         }
    
  30.     }
    
  31.     private static void CheckID()
    
  32.     {
    
  33.         for (int i = 0; i < _EmailStateList.Count; i++)
    
  34.         {
    
  35.             EmailState es = _EmailStateList[i];
    
  36.             for (int j = i + 1; j < _EmailStateList.Count; j++)
    
  37.             {
    
  38.                 if (es.Id == _EmailStateList[j].Id)
    
  39.                 {
    
  40.                     throw new Exception("不能有相同的ID");
    
  41.                 }
    
  42.             }
    
  43.         }
    
  44.     }
    
  45.     public static EmailState GetEmailStateByName(string name)
    
  46.     {
    
  47.         foreach (EmailState es in _EmailStateList)
    
  48.         {
    
  49.             if(es.Name==name)
    
  50.             {
    
  51.                 return es;
    
  52.             }
    
  53.         }
    
  54.         return null;
    
  55.     }
    
  56.     public static EmailState GetEmailStateById(int id)
    
  57.     {
    
  58.         foreach (EmailState es in _EmailStateList)
    
  59.         {
    
  60.             if (es.Id == id)
    
  61.             {
    
  62.                 return es;
    
  63.             }
    
  64.         }
    
  65.         return null;
    
  66.     }
    
  67. 
    
  68.     public static readonly EmailState 未使用 = new EmailState(1, "未使用");
    
  69.     public static readonly EmailState 已使用 = new EmailState(2, "已使用");
    
  70. }
    
  71. public class EmailState
    
  72. {
    
  73.     private int _Id;
    
  74.     private string _Name;
    
  75.     public int Id
    
  76.     {
    
  77.         get{
    
  78.             return _Id;
    
  79.         }
    
  80.     }
    
  81.     public string Name
    
  82.     {
    
  83.         get{
    
  84.             return _Name;
    
  85.         }
    
  86.     }
    
  87.     public EmailState(int id,string name)
    
  88.     {
    
  89.         _Id = id;
    
  90.         _Name = name;
    
  91.     }
    
  92. }
    
<script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i以上是對象枚舉的一個實例代碼,主要是實現郵箱的狀態表示.這樣寫的好處是可以像枚舉一樣訪問對象,並能很方便的將數據綁定到控件上.還有很多其他好處,自己慢慢體會吧
發佈了31 篇原創文章 · 獲贊 3 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章