首页 技术 正文
技术 2022年11月16日
0 收藏 432 点赞 4,349 浏览 2153 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,982
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,499
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,343
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,126
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,760
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,796