日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

.Net Attribute详解(下) - 使用Attribute武装枚举类型

發(fā)布時(shí)間:2025/3/15 asp.net 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .Net Attribute详解(下) - 使用Attribute武装枚举类型 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

接上文.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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。