分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

.Net Attribute详解(上)-Attribute本质以及一个简单示例

发布时间:2023-09-06 01:18责任编辑:董明明关键词:暂无标签

Attribute的直接翻译是属性,这和Property容易产生混淆,所以一般翻译成特性加以区分。Attribute常常的表现形式就是[AttributeName], 随意地添加在class, method的头上,然后就能够产生各种各样奇特的效果和行为。比如关于序列化的标签[Serializable]用来指定一个实体类可以序列化。[NonSerialized]可以用来指定某些属性不包含在序列化中。

一, Attribute本质是什么?

Attribute类似于标签, 可以为类,方法,结构体,属性,委托等贴上标签,在以后的实际执行时候,根据不同的标签做不同的处理。拿类做个比方,如果把一个类看做一个人的话,它有自己的名字Person, 有自己的属性Hand, Foot等,有自己的功能Walk(), Sleep()等,那么加在类上的Attribute就好像是给类这个人穿上的一件外衣。如果我们看到他穿的不同制服,就知道这个人是什么职业,是一个警察,空姐还是快递。

拿上面的所举例的序列化标签[NonSerialized]来说,他就给属性穿上一件外衣,外衣上写着“不要序列化我”,这样在执行具体的序列化的过程中,当序列化操作发现了披着这个外衣对的属性,就会跳过。

二, Attribute的具象

对于Attribute的具体的代码呈现,它有这些特点:

1. Attribute是一个类
自定义的Attribute是一个类,而且必须继承自System.Attribute.

2. Attribute的名字
Attribute类名一般以Attribute结尾, 但是在具体使用时候,可以省去Attribute。加入定义了一个HelperAttribute, 具体使用的时候,可以这样写[Helper].

3. Attribute的使用范围
Attribute类在定义的时候,可以指定这个Attribute的应用范围,AttributeTargets枚举就列出了可以指定的范围,可以是class, field……

[AttributeUsage(AttributeTargets.All)]public class AcronymAttribute : Attribute{}

三, 一个自定义Attribute例子

1. 自定义BlockAttribute

假设有个过滤不当言论的需求,我们可以通过自定义的BlockAttribute为不同类型的人套上外衣,凡是贴上BlockAttribute的人,我们就不允许他发言。
BlockAttribute的代码定义如下:

 ?[AttributeUsage(AttributeTargets.Class)]//指定Attribute的使用范围,只能在class级别使用 ?public class BlockAttribute : Attribute ?{ ?????public Level Level ?????{ ?????????get; ?????????set; ?????} ?????public BlockAttribute(Level level)//在实例化的时候,就指定Block是Yes还是No ?????{ ?????????Level = level; ?????} ?} ???public enum Level ???{ ???????NO, ???????Yes ???}

2. 使用Attribute

接着我们把它使用在我们的GovermentSay类上,它的级别自然是Yes

 ??[Block(Level.Yes)] ??public class GovermentSay : ISay ??{ ??????public string Say() ??????{ ??????????return "Our country is the most democratic country"; ??????} ??}

把BlockAttribute用在PeopleSay类上,级别是No

 ??[Block(Level.NO)] ??public class PeopleSay : ISay ??{ ??????public string Say() ??????{ ??????????return "We need rights"; ??????} ??}

3. Attribute的过滤

接着是我们ThePress类,它的主要作用,就是根据BlockAttribute不同,区别对待。因为它们被BlockAttribute贴上了不同的标签,所以非常容易区分它们。

 ??public class ThePress ??{ ??????public static void Print(ISay say) ??????{ ??????????System.Reflection.MemberInfo info = say.GetType(); ??????????BlockAttribute att= ???????????????(BlockAttribute)Attribute.GetCustomAttribute(info, typeof(BlockAttribute)); ??????????if (att.Level == Level.Yes)//如果标签是Yes ??????????????Console.WriteLine(say.GetType().ToString() + ": " + say.Say()); ??????????else//如果是No ??????????????Console.WriteLine(say.GetType().ToString() + ": " + "I Love the country!"); ??????} ??}

上面就是一个完整的Attribute使用的例子。

四,总结

Attribute使用,一共分三个步骤,Attribute的定义, Attribute的使用(贴标签), Attribute的读取和使用(根据标签做不同处理)
最后,附上本文相关源代码。 AttributeDemo.zip

.Net Attribute详解(上)-Attribute本质以及一个简单示例

原文地址:http://www.cnblogs.com/webenh/p/7691787.html

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved