读取枚举类型的描述信息

    public enum FileCode
    {
        /// <summary>
        /// 申请表
        /// </summary>
        [Description("申请表")]
        A001
     }

public class GetEnumDescription
    {
        public static string GetEnumName<T>(string value) where T : new()
        {
            Type t = typeof (T);
            foreach (MemberInfo mInfo in t.GetMembers())
            {
                var str = t.GetEnumNames();
                var query = str.Where(e => e.Equals(value));
                if (query.Any() && mInfo.Name == query.ToList()[0])
                {
                    foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                    {
                        if (attr.GetType() == typeof (DescriptionAttribute))
                        {
                            return ((DescriptionAttribute) attr).Description;
                        }
                    }
                }
            }
            return "";
        }
    }

通过反射读取枚举类型的描述信息

posted on 2016-02-23 14:13  blog_zt  阅读(319)  评论(0编辑  收藏  举报

导航