root/UnityWeb/UnityWeb/Modules/Common/EnumerationParser.cs

User picture

Author: marisic.net

Revision: 112 («Previous)


File Size: 3.62 KB

(February 02, 2009 07:47 UTC) Over 3 years ago


  

 
Show/hide line numbers
#region Using Statements

using System;
using System.ComponentModel;

#endregion

namespace Modules.Common
{
    ///<summary>
    /// Handles operations for Enumerations
    ///</summary>
    public static class EnumerationParser
    {
        /// <summary>
        /// Reads the description attribute tag from an enum.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string ReadDescription<T>(T value)
        {
            var type = typeof (T);
            if (IsNullable(type))
                type = UnderlyingTypeOf(type);

            var fi = type.GetField(value.ToString());
            var attributes = (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
            return attributes.Length > 0 ? attributes[0].Description : value.ToString();
        }

        /// <summary>
        /// Parses the enum by description.
        /// </summary>
        /// <param name="descriptionValue">The description value.</param>
        /// <returns></returns>
        public static T ParseEnumByDescription<T>(string descriptionValue)
        {
            var type = typeof (T);
            if (IsNullable(type))
                type = UnderlyingTypeOf(type);

            var names = Enum.GetNames(type);
            foreach (string name in names)
            {
                var compare = Parse<T>(name);

                if (ReadDescription(compare).Equals(descriptionValue, StringComparison.InvariantCultureIgnoreCase))
                    return compare;
            }

            throw new ArgumentException("The string is not a description or value of the specified enum.");
        }

        /// <summary>
        /// Parses any string to the type of T: Enum
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static T Parse<T>(string value)
        {
            return Parse<T>(value, false);
        }

        /// <summary>
        /// Parses any string to the type of T: Enum
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">The value.</param>
        /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
        /// <returns></returns>
        public static T Parse<T>(string value, bool ignoreCase)
        {
            if (string.IsNullOrEmpty(value) || string.Empty.Equals(value.Trim(), StringComparison.InvariantCultureIgnoreCase))
                return default(T);

            var type = typeof (T);
            if (IsNullable(type))
                type = UnderlyingTypeOf(type);

            return (T) Enum.Parse(type, value.Trim(), ignoreCase);
        }

        /// <summary>
        /// Determines whether the specified t is nullable.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns>
        /// 	<c>true</c> if the specified t is nullable; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsNullable(Type t)
        {
            if (!t.IsGenericType)
                return false;
            var g = t.GetGenericTypeDefinition();
            return (g.Equals(typeof (Nullable<>)));
        }

        /// <summary>
        /// Returns the underlying Type of T
        /// </summary>
        /// <param name="t">the type</param>
        /// <returns></returns>
        private static Type UnderlyingTypeOf(Type t)
        {
            return t.GetGenericArguments()[0];
        }
    }
}