示例#1
0
        /// <summary>
        /// Turns an enum member into a human readable string; e.g. AnonymousUser -> Anonymous user. It also honors DescriptionAttribute data annotation
        /// </summary>
        /// <param name="input">The enum member to be humanized</param>
        /// <returns></returns>
        public static string Humanize(this Enum input)
        {
            var type = input.GetType();

            if (IsBitFieldEnum(input) && !DirectlyMapsToEnumConstant(input))
            {
                var inputIntValue = Convert.ToInt32(input);

                var humanizedEnumValues = new List <string>();

                foreach (var enumValue in Enum.GetValues(type))
                {
                    var enumIntValue = (int)enumValue;

                    if ((inputIntValue & enumIntValue) != 0)
                    {
                        humanizedEnumValues.Add(EnumHumanizeExtensions.Humanize((Enum)enumValue));
                    }
                }

                return(CollectionHumanizeExtensions.Humanize <string>(humanizedEnumValues));
            }

            var caseName = input.ToString();
            var memInfo  = type.GetTypeInfo().GetDeclaredField(caseName);

            if (memInfo != null)
            {
                var customDescription = GetCustomDescription(memInfo);

                if (customDescription != null)
                {
                    return(customDescription);
                }
            }

            return(caseName.Humanize());
        }
        // Token: 0x06000040 RID: 64 RVA: 0x00002450 File Offset: 0x00000650
        public static string Humanize(this Enum input)
        {
            Type     enumType     = input.GetType();
            TypeInfo enumTypeInfo = enumType.GetTypeInfo();

            if (EnumHumanizeExtensions.IsBitFieldEnum(enumTypeInfo) && !Enum.IsDefined(enumType, input))
            {
                return((from Enum e in Enum.GetValues(enumType)
                        where input.HasFlag(e)
                        select e.Humanize()).Humanize <string>());
            }
            string    caseName = input.ToString();
            FieldInfo memInfo  = enumTypeInfo.GetDeclaredField(caseName);

            if (memInfo != null)
            {
                string customDescription = EnumHumanizeExtensions.GetCustomDescription(memInfo);
                if (customDescription != null)
                {
                    return(customDescription);
                }
            }
            return(caseName.Humanize());
        }