在C#中如何实现枚举类型的特性扩展和反射获取

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(); 

             }   
  } 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值