接上文.Net Attribute详解(上)-Attribute本质以及一个简单示例,这篇文章介绍一个非常实用的例子,相信你一定能够用到你正在开发的项目中。枚举类型被常常用到项目中,如果要使用枚举ToString方法直接输出字符串, 常常不是我们想要的输出,因为它是安装定义的名称输出字符串。比如你有一个性别枚举,有Man, Woman. 你在中文系统中,在创建用户的页面上,这个枚举代表的下拉框,当然不是显示Man和Woman的,而是要显示”男”和”女“。 下面就介绍如何使用Attribute非常方便的输出我们想要的字符串。
1, 使用System.ComponentModel.DescriptionAttribute
比如,下面这个枚举
enum Gender
{
Man,
Woman
};
在使用上DescriptionAttribute后,可以改造成这样
enum Gender
{
[Description(“男”)]
Man,
[Description(“女”)]
Woman
};
好了,使用Attribute的三个步骤:
Attribute的定义, Attribute的使用(贴标签), Attribute的读取和使用(根据标签做不同处理)
第一步,我们使用了系统中的Attribute,贴标签已经做好了,接下来时对于Attribute的读取和使用了。
2, EnumHelper类
下面定义的EnumHelper类,使用扩展方法,为枚举提供了非常方便的方式输出Description. 比如,我们可以这样使用下面的方法,来得到对应项的字符串:
Gender.Man.GetDescription()
上面输出的就会我们想要的”男”, 而不是”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的原理完全相同。
还有关于Model上的验证相关的Attribute, 比如MaxLength.
[MaxLength(100, ErrorMessage = "The max length is 100")]
public string Name{get;set;}
把MaxLength加在Name上,它不仅能够作为MVC中的验证,而且会在Entity Framwork中,作为数据检查规则。因为这个属性披上了一个外衣,上面写着“我的最大长度是100”,如果有任何数据有效性检查的类,都会看到这个外衣,同时检查这个属性的长度是否符合要求。
总之,理解了什么是Attribute, 以及原理,对于理解.net中无处不在的,那些加载类上或者属性上的[]东东,一定能够更加容易。
| 本博客Android APP 下载 |
![]() |
| 支持我们就给我们点打赏 |
![]() |
| 支付宝打赏 支付宝扫一扫二维码 |
![]() |
| 微信打赏 微信扫一扫二维码 |
![]() |
如果想下次快速找到我,记得点下面的关注哦!





浙公网安备 33010602011771号