示例#1
0
        /// <summary>
        ///		Gets the description from an enum (using System.ComponentModel to utilize the DescriptionAttribute and System.Reflection to get the Attribute's value).
        ///		For iOS (or for the case of no Description attribute), defaults to the string value of the enum.
        ///		NOTE: Is not able to differentiate between two enum entries that share the same value.
        ///			If differentiation is required, use EnumUtility.GetDescriptions() instead.
        /// </summary>
        /// <param name="enumValue">The enum to return a Description for.</param>
        /// <param name="userFriendlyAsFallback">If true, uses a user-friendly text format if there is no description.  See FormatUtility.GetUserFriendlyText.</param>
        /// <returns>string, either being the description, a user-friendly fallback, or the ToString representation.</returns>
        public static string GetDescription(this Enum enumValue, bool userFriendlyAsFallback = true)
        {
                        #if !UNITY_IOS
            Type   type = enumValue.GetType();
            string name = Enum.GetName(type, enumValue);
            if (name != null)
            {
                FieldInfo field = type.GetField(name);
                if (field != null)
                {
                    System.ComponentModel.DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(System.ComponentModel.DescriptionAttribute)) as System.ComponentModel.DescriptionAttribute;
                    if (attr != null)
                    {
                        return(attr.Description);
                    }
                }
            }
                        #endif

            if (userFriendlyAsFallback)
            {
                return(FormatUtility.GetUserFriendlyText(enumValue.ToString()));
            }
            return(enumValue.ToString());
        }
示例#2
0
        /// <summary>
        ///		Returns a list of enum Descriptions (using the System.ComponentModel.DescriptionAttribute), ordered by index in the enum.
        ///		NOTE: This will preserve duplicate entries (multiple enum entries mapped to the same value), unlike Enum.GetDescription().
        /// </summary>
        /// <exception cref="ArgumentException">Passed Type is not an enum.</exception>
        /// <param name="enumType"></param>
        /// <param name="userFriendlyAsFallback">
        ///		If true, uses a user-friendly text format if there is no description.  See FormatUtility.GetUserFriendlyText.
        ///	</param>
        /// <returns></returns>
        public static List <string> GetDescriptions(Type enumType, bool userFriendlyAsFallback = true)
        {
            if (!enumType.IsEnum)
            {
                throw new ArgumentException("Passed Type \"" + enumType.ToString() + "\" is not an enum.");
            }

            List <string> names;

            if (!storedDescriptions.TryGetValue(enumType, out names))
            {
                names = new List <string>(Enum.GetNames(enumType));

                                #if !UNITY_IOS
                FieldInfo field;

                for (int i = 0; i < names.Count; i++)
                {
                    field = (enumType).GetField(names[i]);
                    DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attr != null)
                    {
                        names[i] = attr.Description;
                    }
                    else if (userFriendlyAsFallback)
                    {
                        names[i] = FormatUtility.GetUserFriendlyText(names[i]);
                    }
                }
                                #else
                if (userFriendlyAsFallback)
                {
                    for (int i = 0; i < names.Count; i++)
                    {
                        names[i] = FormatUtility.GetUserFriendlyText(names[i]);
                    }
                }
                                #endif

                storedDescriptions.Add(enumType, names);
            }
            return(new List <string>(names));
        }