C# 枚舉類型 enum

我我的感受平日用到的enum應該是很是簡單的,無非就是枚舉和整數、字符串之間的轉換。最近工做發現一些同事竟然不太會用這個東東,因而就整理一下。緩存

枚舉類型是定義了一組「符號名稱/值」配對。枚舉類型是強類型的。每一個枚舉類型都是從system.Enum派生,又從system.ValueType派生,而system.ValueType又從system.Object派生,因此枚舉類型是值類型。編譯枚舉類型時,C#編譯器會把每一個符號轉換成類型的一個常量字段。C#編譯器將枚舉類型視爲基元類型。由於enum是值類型,而全部的值類型都是sealed,因此在擴展方法時都不能進行約束。可是,若要限制參數爲值類型,可用struct 約束。由於enum通常用於一個字符串和整數值,若是咱們須要關聯多個字符串的時候可使用Description特性,若是枚舉彼此須要與或運算,須要藉助Flags特性,enum裏面定義的字符常量其整數值默認從0開始性能

看一下枚舉定義:this

 public enum AwardType
    {
        /// <summary>
        ///1 積分
        /// </summary>
        [Description("積分")]
        Integral = 1,

        /// <summary>
        /// 2 充值卡
        /// </summary>
        [Description("充值卡")]
        RechargeCard = 2,

        /// <summary>
        /// 3 實物獎品
        /// </summary>
        [Description("實物獎品")]
        Gift = 3,
    }
    public static class Extend
    {
        public static string Description<T>(this T instance) where T : struct
        {
            string str = string.Empty;
            Type type = typeof(T);
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("參數必須是枚舉類型");
            }
            var fieldInfo = type.GetField(instance.ToString());
            if (fieldInfo != null)
            {
                var attr = fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
                if (attr != null)
                {
                    str = attr.Description;
                }
                else
                {
                    str = fieldInfo.Name;
                }

            }
            return str;
        }

        public static T GetEnum<T>(this string description)
        {
            var fieldInfos = typeof(T).GetFields();
            foreach (FieldInfo field in fieldInfos)
            {
                DescriptionAttribute attr = field.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
                if (attr != null && attr.Description == description)
                {
                    return (T)field.GetValue(null);
                }
                else if (field.Name == description)
                {
                    return (T)field.GetValue(null);
                }
            }
            throw new ArgumentException(string.Format("{0} 未能找到對應的枚舉.", description), "Description");
        }
    }

編譯後以下:編碼

在編碼的時候 必要的註釋 不能缺乏,以下spa

/// <summary>
/// 3 實物獎品
/// </summary>3d

這樣VS在只能感知的時候就能夠看到了。code

而枚舉的應用也很是簡單:orm

  static void Main(string[] args)
        {
            //枚舉與整數轉換
            int giftInt = (int)AwardType.Gift;
            var a = (AwardType)giftInt;
            //枚舉與字符串轉換        
            string giftStr = AwardType.Gift.ToString();
            Enum.TryParse(giftStr, out AwardType b);
            ///檢測枚舉是否有效
            bool isdefine = Enum.IsDefined(typeof(AwardType), 1);
            ////獲取枚舉描述
            string str = AwardType.Gift.Description();
            ////經過描述獲取枚舉
            AwardType award = str.GetEnum<AwardType>();
            Console.ReadKey();
        }

這裏枚舉和其描述信息是直接經過反射來實現的,爲了性能提升能夠採用字典緩存數據blog

相關文章
相關標籤/搜索