在C#中如何?枚舉類型的特性擴充和反射擷取

來源:互聯網
上載者:User

public enum 月定製項目
  {
            [CustomItemAttribute()]
            一點五元夜餐次數=1,
            [CustomItemAttribute()]
            二元夜餐次數=2,
            [CustomItemAttribute()]
             連班次數 =3,
            [CustomItemAttribute()]
            甲類保健津貼次數 =4,
            [CustomItemAttribute()]
            乙類保健津貼次數 =5,
            [CustomItemAttribute()]
            周六加班次數 =6,
            [CustomItemAttribute()]
            周日加班次數 =7,
            [CustomItemAttribute()]
            零點夜班次數 =8,
            [CustomItemAttribute()]
            節假日加班次數 =9,
            [CustomItemAttribute()]
            四點夜班次數 =10,
            [CustomItemAttribute()]
            事假天數=11,
            [CustomItemAttribute()]
            病假天數 =12,
            [CustomItemAttribute()]
            曠工天數 =13,
            [CustomItemAttribute()]
            公差天數 =14,
            [CustomItemAttribute()]
            探親天數 =15,
            [CustomItemAttribute(CustomApplyType.工區員工)]
            效益工資 = 16
  } 
  public enum CustomApplyType
  {
            工區員工 = 1,
            機關員工 = 2,
            所有員工 = 3
  }
  [AttributeUsage(AttributeTargets.Field)]
  public class CustomItemAttribute:System.Attribute
  {
            protected CustomApplyType _customApplyType;
            public CustomItemAttribute(CustomApplyType customApplyType)
            {
                        this._customApplyType = customApplyType;
            }
            public CustomItemAttribute():this(CustomApplyType.所有員工)
            {
            }
            public CustomApplyType CustomApplyType{get{return this._customApplyType;}}
  } 
  class Application
  {
            [STAThread]
            static void Main(string[] args)
            {
                        FieldInfo[] fs = typeof(月定製項目).GetFields();
                        object enumInstance = typeof(月定製項目).Assembly.CreateInstance(typeof(月定製項目).FullName);
                        foreach(FieldInfo f in fs)
                        {
                                                CustomItemAttribute[] cas = (CustomItemAttribute[])f.GetCustomAttributes(typeof(CustomItemAttribute),true);
                                                if(cas.Length >0)
                                                {     
                                                            Console.WriteLine(f.GetValue(enumInstance));
                                                            Console.WriteLine(Convert.ToSingle((int)f.GetValue(enumInstance)));
                                                            Console.WriteLine(cas[0].CustomApplyType.ToString());
                                                }
                         }
                        Console.Read(); 

             }   
  } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.