Exemple #1
0
        /// <summary>
        /// 按照描述值转换成枚举值
        /// </summary>
        /// <typeparam name="T">枚举类型名称</typeparam>
        /// <param name="displayNameOrDesc">描述值</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>枚举值</returns>
        public static T ParserEnumByDisplayNameOrDesc <T>(string displayNameOrDesc, T defaultValue)
            where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T必须是枚举类型");
            }

            if (string.IsNullOrEmpty(displayNameOrDesc))
            {
                return(defaultValue);
            }

            foreach (MemberInfo member in typeof(T).GetMembers())
            {
                if (PropertyWrapper.GetDisplayAttributeName(member).Equals(displayNameOrDesc) || PropertyWrapper.GetDisplayAttributeDesc(member).Equals(displayNameOrDesc))
                {
                    return(ParserEnumByName(member.Name, defaultValue));
                }
            }

            return(defaultValue);
        }