Enum擴充特性執行個體代碼

來源:互聯網
上載者:User
由於對英語的天生缺陷,在枚舉時一直使用中文,這樣就不用看注釋就知道枚舉意思,今天看到博文

使用特性代替了直接使用中文作為屬性。特意摘抄部分為以後使用方便

    /// <summary>/// 枚舉協助類/// </summary>public static class EnumTools    {/// <summary>///  擷取當前枚舉值的描述和排序/// </summary>/// <param name="value"></param>/// <returns>返回元組Tuple(string,int)</returns>public static Tuple<string,int> GetDescription(this Enum value)        {int order = 0;string description = string.Empty;            Type type = value.GetType();// 擷取枚舉FieldInfo fieldInfo = type.GetField(value.ToString());// 擷取枚舉自訂的特性DescriptionAttributeobject[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);            DescriptionAttribute attr = (DescriptionAttribute)attrs.FirstOrDefault(a => a is DescriptionAttribute);            description = fieldInfo.Name;if (attr != null)            {                order = attr.Order;                description = attr.Name;            }return new Tuple<string,int>(description,order);        }/// <summary>/// 擷取當前枚舉的所有描述/// </summary>/// <returns></returns>public static List<KeyValuePair<int, string>> GetAll<T>()        {return GetAll(typeof(T));        }/// <summary>/// 擷取所有的枚舉描述和值/// </summary>/// <param name="type"></param>/// <returns></returns>public static List<KeyValuePair<int, string>> GetAll(Type type)        {            List<EnumToolsModel> list = new List<EnumToolsModel>();// 迴圈枚舉擷取所有的Fieldsforeach (var field in type.GetFields())            {// 如果是枚舉類型if (field.FieldType.IsEnum)                {object tmp = field.GetValue(null);                    Enum enumValue = (Enum)tmp;int intValue = Convert.ToInt32(enumValue);var dec = enumValue.GetDescription();int order= dec.Item2;string showName = dec.Item1; // 擷取描述和排序list.Add(new EnumToolsModel { Key = intValue, Name = showName, Order = order });                }            }// 排序並轉成KeyValue返回return list.OrderBy(i => i.Order).Select(i => new KeyValuePair<int, string>(i.Key, i.Name)).ToList();        }/// <summary>/// 枚舉Model/// </summary> partial class EnumToolsModel        {public int Order { get; set; }public string Name { get; set; }public int Key { get; set; }        }    }/// <summary>/// 枚舉特性/// </summary>[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]public class DescriptionAttribute : Attribute    {/// <summary>/// 排序/// </summary>public int Order { get; set; }/// <summary>/// 名稱/// </summary>public string Name { get; set; }/// <summary>/// 定義描述名稱/// </summary>/// <param name="name">名稱</param>public DescriptionAttribute(string name)        {            Name = name;        }/// <summary>/// 定義描述名稱和排序/// </summary>/// <param name="name">名稱</param>/// <param name="order">排序</param>public DescriptionAttribute(string name, int order)        {            Name = name;            Order = order;        }    }
View Code

把原文中的out參數替換成返回元組,由於項目是vs2015開發,不能用c#7.0特性,否則用7.0中的值元組應該更好一點。效能和顯示友好性都會有改進。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.