反射获取枚举的属性注释

 public enum AccountType
    {       
        [Description("支付宝充值")]
        ALIPAY = 1,
  
        [Description("银联充值")]
        UNIONPAY = 2

}

public static string GetEnumName<T>(int value) where T : new()
        {
            Type t = typeof(T);
            foreach (MemberInfo mInfo in t.GetMembers())
            {
                if (mInfo.Name == t.GetEnumName(value))
                {
                    foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                    {
                        if (attr.GetType() == typeof(DescriptionAttribute))
                        {
                            return ((DescriptionAttribute)attr).Description;
                        }
                    }
                }
            }
            return "";
        }

posted @ 2013-12-03 17:28  快乐就好  阅读(2614)  评论(0编辑  收藏  举报