反射获取枚举的属性注释

  1. 云栖社区>
  2. 博客>
  3. 正文

反射获取枚举的属性注释

老先生二号 2018-01-13 17:15:34 浏览984
展开阅读全文

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 "";
        }


本文转自快乐就好博客园博客,原文链接:http://www.cnblogs.com/happyday56/p/3456195.html,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论