Beispiel #1
0
        /// <summary>
        /// Get the enum description.
        /// </summary>
        /// <param name="enumType">the enum type</param>
        /// <param name="enumValue">the enum value</param>
        /// <exception>TypeCompatibilityException</exception>
        private static string GetDescriptionWithoutCache(Type enumType, object enumValue)
        {
            Guard.EnumValueIsDefined(enumType, enumValue, "enumValue");
            bool isFlagEnum  = enumType.HasAttribute <FlagsAttribute>();
            Type underlyType = Enum.GetUnderlyingType(enumType);

            var           names       = Enum.GetNames(enumType);
            StringBuilder currentName = new StringBuilder();

            foreach (string name in names)
            {
                object value = Enum.Parse(enumType, name, false);

                if (!isFlagEnum) //普通枚举。
                {
                    if (value.Equals(enumValue))
                    {
                        currentName = new StringBuilder(name);
                        FieldInfo enumField = enumType.GetTypeInfo().GetField(name);
                        EnumDescriptionAttribute attribute = enumField.GetCustomAttribute <EnumDescriptionAttribute>();
                        if (attribute != null)
                        {
                            return(attribute.GetDescription().IfNullOrWhiteSpace(enumValue.ToString()));
                        }
                    }
                }
                else //位枚举。
                {
                    if (((Enum)enumValue).HasFlag((Enum)value))
                    {
                        if (Convert.ToInt64(value) == 0 && Convert.ToInt64(enumValue) != 0)
                        {
                            continue;
                        }
                        string    text      = name;
                        FieldInfo enumField = enumType.GetTypeInfo().GetField(name);
                        EnumDescriptionAttribute attribute = enumField.GetCustomAttribute <EnumDescriptionAttribute>();
                        if (attribute != null)
                        {
                            text = attribute.GetDescription().IfNullOrWhiteSpace(value.ToString());
                        }
                        currentName.Append(String.Format("{0}{1}", text, FlagSplitCharacter));
                    }
                }
            }

            if (isFlagEnum && currentName.Length > 0 && !FlagSplitCharacter.IsNullOrWhiteSpace())
            {
                currentName.Remove(currentName.Length - FlagSplitCharacter.Length, FlagSplitCharacter.Length);
            }
            return(currentName.ToString());
        }
        /// <summary>
        /// Get the enum description.
        /// </summary>
        /// <param name="enumType">the enum type</param>
        /// <param name="enumValue">the enum value</param>
        /// <exception>TypeCompatibilityException</exception>
        private static string GetDescriptionWithoutCache(Type enumType, object enumValue)
        {
            Guard.EnumValueIsDefined(enumType, enumValue, "enumValue");
            var isFlagEnum = enumType.HasAttribute <FlagsAttribute>();

            var names       = Enum.GetNames(enumType);
            var currentName = new StringBuilder();

            foreach (var name in names)
            {
                var value = Enum.Parse(enumType, name, false);

                if (!isFlagEnum) //普通枚举。
                {
                    currentName = new StringBuilder(name);
                    var attribute = GetOrdinaryValue(value, enumValue, name, enumType);
                    if (attribute == null)
                    {
                        continue;
                    }
                    return(attribute.GetDescription().IfNullOrWhiteSpace(enumValue.ToString()));
                }
                else //位枚举。
                {
                    var text      = name;
                    var attribute = GetFlagValue(value, enumValue, text, enumType);
                    if (attribute == null)
                    {
                        continue;
                    }
                    text = attribute.GetDescription().IfNullOrWhiteSpace(value.ToString());
                    currentName.Append($"{text}{FlagSplitCharacter}");
                }
            }

            if (isFlagEnum && currentName.Length > 0 && !FlagSplitCharacter.IsNullOrWhiteSpace())
            {
                currentName.Remove(currentName.Length - FlagSplitCharacter.Length, FlagSplitCharacter.Length);
            }
            return(currentName.ToString());
        }