反射获取枚举的属性注释

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,如需转载请自行联系原作者
上一篇:linux 查找目录或文件详解


下一篇:Linux Mutex机制分析