.Net Attribute详解(下) - 使用Attribute武装枚举类型
接上文.Net Attribute詳解(上)-Attribute本質(zhì)以及一個(gè)簡(jiǎn)單示例,這篇文章介紹一個(gè)非常實(shí)用的例子,相信你一定能夠用到你正在開發(fā)的項(xiàng)目中。枚舉類型被常常用到項(xiàng)目中,如果要使用枚舉ToString方法直接輸出字符串, 常常不是我們想要的輸出,因?yàn)樗前惭b定義的名稱輸出字符串。比如你有一個(gè)性別枚舉,有Man, Woman. 你在中文系統(tǒng)中,在創(chuàng)建用戶的頁(yè)面上,這個(gè)枚舉代表的下拉框,當(dāng)然不是顯示Man和Woman的,而是要顯示”男”和”女“。 下面就介紹如何使用Attribute非常方便的輸出我們想要的字符串。
1, 使用System.ComponentModel.DescriptionAttribute
比如,下面這個(gè)枚舉
enum Gender {Man,Woman };在使用上DescriptionAttribute后,可以改造成這樣
enum Gender {[Description(“男”)]Man,[Description(“女”)]Woman };好了,使用Attribute的三個(gè)步驟:
Attribute的定義, Attribute的使用(貼標(biāo)簽), Attribute的讀取和使用(根據(jù)標(biāo)簽做不同處理)
第一步,我們使用了系統(tǒng)中的Attribute,貼標(biāo)簽已經(jīng)做好了,接下來時(shí)對(duì)于Attribute的讀取和使用了。
2, EnumHelper類
下面定義的EnumHelper類,使用擴(kuò)展方法,為枚舉提供了非常方便的方式輸出Description. 比如,我們可以這樣使用下面的方法,來得到對(duì)應(yīng)項(xiàng)的字符串:
Gender.Man.GetDescription()
上面輸出的就會(huì)我們想要的”男”, 而不是”Man”.
/// <summary>/// Contains methods for working with <see cref="Enum"/>./// </summary>public static class EnumHelper{/// <summary>/// Gets the specified enum value's description./// </summary>/// <param name="value">The enum value.</param>/// <returns>The description or <c>null</c>/// if enum value doesn't have <see cref="DescriptionAttribute"/>.</returns>public static string GetDescription(this Enum value){var fieldInfo = value.GetType().GetField(value.ToString());var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);return attributes.Length > 0? attributes[0].Description: null;}/// <summary>/// Gets the enum value by description./// </summary>/// <typeparam name="EnumType">The enum type.</typeparam>/// <param name="description">The description.</param>/// <returns>The enum value.</returns>public static EnumType GetValueByDescription<EnumType>(string description){var type = typeof(EnumType);if (!type.IsEnum)throw new ArgumentException("This method is destinated for enum types only.");foreach (var enumName in Enum.GetNames(type)){var enumValue = Enum.Parse(type, enumName);if (description == ((Enum)enumValue).GetDescription())return (EnumType)enumValue;}throw new ArgumentException("There is no value with this description among specified enum type values.");}}3.? Attribute在MVC中的Razor view engine 中的使用
在Model上我們可以添加上DisplayAttribute, 比如
public class User {[Display(Name = "User Name)]public string Name{get;set;} }這樣在view中使用Html.DisplayFor(), 輸出的就是User Name。
這里的使用和我們上面的EnumHelper的原理完全相同。
還有關(guān)于Model上的驗(yàn)證相關(guān)的Attribute, 比如MaxLength.
[MaxLength(100, ErrorMessage = "The max length is 100")] public string Name{get;set;}把MaxLength加在Name上,它不僅能夠作為MVC中的驗(yàn)證,而且會(huì)在Entity Framwork中,作為數(shù)據(jù)檢查規(guī)則。因?yàn)檫@個(gè)屬性披上了一個(gè)外衣,上面寫著“我的最大長(zhǎng)度是100”,如果有任何數(shù)據(jù)有效性檢查的類,都會(huì)看到這個(gè)外衣,同時(shí)檢查這個(gè)屬性的長(zhǎng)度是否符合要求。
?總之,理解了什么是Attribute, 以及原理,對(duì)于理解.net中無處不在的,那些加載類上或者屬性上的[]東東,一定能夠更加容易。
本文轉(zhuǎn)自JustRun博客園博客,原文鏈接:http://www.cnblogs.com/JustRun1983/p/3466773.html,如需轉(zhuǎn)載請(qǐng)自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的.Net Attribute详解(下) - 使用Attribute武装枚举类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 请尽快升级到 Windows Vista
- 下一篇: ajax核心技术1---XMLHttpR