一つ紹介します.NET列挙共通クラス

5031 ワード

using System;
using System.ComponentModel;
using System.Reflection;

namespace Common
{
    /**//// <summary>
    ///      
    /// </summary>
    public static class EnumHelper
    {

        private static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
        {
            if (fieldInfo != null)
            {
                return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            }
            return null;
        }


        /**//// <summary>
        ///              
        /// </summary>
        /// <typeparam name="T">  </typeparam>
        /// <param name="enumName">      </param>
        /// <returns>          </returns>

        public static bool CheckedContainEnumName<T>(string enumName) where T : struct, IConvertible
        {
            bool _result = false;
            if (typeof(T).IsEnum)
            {
                string[] _enumnName = Enum.GetNames(typeof(T));
                if (_enumnName != null)
                {
                    for (int i = 0; i < _enumnName.Length; i++)
                    {
                        if (string.Compare(_enumnName[i], enumName, true) == 0)
                        {
                            _result = true;
                            break;
                        }
                    }
                }
            }
            return _result;
        }

        /**//// <summary>
        ///       Description
        /// </summary>
        /// <param name="targetEnum">           </param>
        /// <returns>    </returns>
        public static string GetDescription(this Enum targetEnum)
        {
            string _description = string.Empty;
            FieldInfo _fieldInfo = targetEnum.GetType().GetField(targetEnum.ToString());
            DescriptionAttribute[] _attributes = _fieldInfo.GetDescriptAttr();
            if (_attributes != null && _attributes.Length > 0)
                _description = _attributes[0].Description;
            else
                _description = targetEnum.ToString();
            return _description;
        }

        /**//// <summary>
        ///   Description    
        /// </summary>
        /// <typeparam name="T">    </typeparam>
        /// <param name="description">    </param>
        /// <returns>  </returns>
        public static T ParseEnumDescription<T>(this string description, T defaultValue) where T : struct, IConvertible
        {
            if (typeof(T).IsEnum)
            {
                Type _type = typeof(T);
                foreach (FieldInfo field in _type.GetFields())
                {
                    DescriptionAttribute[] _description = field.GetDescriptAttr();
                    if (_description != null && _description.Length > 0)
                    {
                        if (string.Compare(_description[0].Description, description, true) == 0)
                        {
                            defaultValue = (T)field.GetValue(null);
                            break;
                        }
                    }
                    else
                    {
                        if (string.Compare(field.Name, description, true) == 0)
                        {
                            defaultValue = (T)field.GetValue(null);
                            break;
                        }
                    }
                }
            }
            return defaultValue;
        }

        /**//// <summary>
        ///             
        /// </summary>
        /// <typeparam name="T">  </typeparam>
        /// <param name="enumName">      </param>
        /// <returns></returns>

        public static T ParseEnumName<T>(this string enumName) where T : struct, IConvertible
        {
            return (T)Enum.Parse(typeof(T), enumName, true);
        }
    }
}

呼び出しインスタンス
public enum AreaMode
    {
        NONE,
        CITY,
        TOWN,
        ROAD,
        CITYTOWN,
        TOWNROAD,
        CITYROAD,
        ALL
    }

    public class EnumHelperTests
    {
        public void CheckedContainEnumNameTest()
        {
            EnumHelper.CheckedContainEnumName<AreaMode>("none");
        }

        public void GetDescriptionTest()
        {
            AreaMode.NONE.GetDescription();
        }

        
        public void ParseEnumDescriptionTest()
        {
            EnumHelper.ParseEnumDescription<AreaMode>("NONE", AreaMode.CITYTOWN);
        }

        public void ParseEnumNameTest()
        {
            EnumHelper.ParseEnumName<AreaMode>("ALL");
        }
    }

参考資料:一つ紹介する.NET列挙共通クラスhttp://www.studyofnet.com/news/1129.html