一、什么是特性?

特性(attribute)是被指定给某一声明的一则附加的声明性信息。

在C#中,有一个小的预定义特性集合。在学习如何建立我们自己的定制特性(custom attributes)之前,我们先来看看在我们的代码中如何使用预定义特性。

using System;
public class AnyClass
{
[Obsolete("Don't use Old method, use New method", true)]
static void Old( ) { }
static void New( ) { }
public static void Main( )
{
Old( );
}
}

我们先来看一下上面这个例子,在这个例子中我们使用了Obsolete特性,它标记了一个不应该再被使用的程序实体。第一个参数是一个字符串,它解释了为什么该实体是过时的以及应该用什么实体来代替它。实际上,你可以在这里写任何文本。第二个参数告诉编译器应该把使用这个过时的程序实体当作一种错误。它的默认值是false,也就是说编译器对此会产生一个警告。

当我们尝试编译上面这段程序的时候,我们将会得到一个错误:   

AnyClass.Old()' is obsolete: 'Don't use Old method, use New method'

二、自定义特性

除了C#中系统自带的特性外我们可以自己定义一些特性。所有自定义的Attribute必须从Attribute类派生,命名也是要以Attribute结尾,在使用的时候可以省略Attribute。

特性类的使用过程:

第一步:定义一个特性类,定义一些成员来包含验证时需要的数据;

第二步:创建特性类实例;

创建一个特性类的实例,里面包含着验证某一个属性或者字段需要的数据。

将该实例关联到某个属性上面。

第三步:使用特性类实例

可以通过调用某个类型的GetProperties()方法,获取属性,

然后调用类型属性成员的GetCustomAttributes()方法,获取该属性关联的特性类实例,

然后使用查找到的特性类实例验证新建对象。

第一步:定义一个特性类,定义一些成员来包含验证时需要的数据;


第二步:创建一个特性类的实例,并关联一个属性

public class Order
{
[StringLength("订单号", , MinLength = , ErrorMessage = "{0}的长度必须在{1}和{2}之间!")]//实例化一个特性类,关联到一个字段上面
public string OrderID { get; set; }
}

第三步:使用特性类实例,进行验证

class Program
{
#region 使用特性类的过程 //验证过程
//1.通过映射,找到成员属性关联的特性类实例,
//2.使用特性类实例对新对象的数据进行验证 //用特性类验证订单号长度
public static bool isIDLengthValid(int IDLength, MemberInfo member)
{
foreach (object attribute in member.GetCustomAttributes(true)) //2.通过映射,找到成员属性上关联的特性类实例,
{
if (attribute is StringLengthAttribute)//3.如果找到了限定长度的特性类对象,就用这个特性类对象验证该成员
{
StringLengthAttribute attr = (StringLengthAttribute)attribute;
if (IDLength < attr.MinLength || IDLength > attr.MaxLength)
{
string displayName = attr.DisplayName;
int maxLength = attr.MaxLength;
int minLength = attr.MinLength;
string error = attr.ErrorMessage;
Console.WriteLine(error, displayName,maxLength, minLength);//验证失败,提示错误
return false;
}
else return true;
} }
return false;
} //验证订单对象是否规范
public static bool isOrderValid(Order order)
{
if (order == null) return false;
foreach (PropertyInfo p in typeof(Order).GetProperties())
{
if (isIDLengthValid(order.OrderID.Length, p))//1记录下新对象需要验证的数据,
return true;
}
return false; }
#endregion
public static void Main()
{
Order order=new Order();
do
{
Console.WriteLine("请输入订单号:");
order.OrderID= Console.ReadLine();
}
while (!isOrderValid(order));
Console.WriteLine("订单号输入正确,按任意键退出!");
Console.ReadKey();
} }

总结:特性类的实例里没有验证逻辑,只有验证用到的规范数据(比如字符串长度)、提示信息等。验证逻辑需要自己写。

三、定义或控制特性的使用

AttributeUsage类是另外一个预定义特性类,它帮助我们控制我们自己的定制特性的使用。它描述了一个定制特性如和被使用。

AttributeUsage有三个属性,我们可以把它放置在定制属性前面。第一个属性是:

ValidOn

通过这个属性,我们能够定义定制特性应该在何种程序实体前放置。一个属性可以被放置的所有程序实体在AttributeTargets enumerator中列出。通过OR操作我们可以把若干个AttributeTargets值组合起来。

AllowMultiple

这个属性标记了我们的定制特性能否被重复放置在同一个程序实体前多次。

Inherited

我们可以使用这个属性来控制定制特性的继承规则。它标记了我们的特性能否被继承。

下面让我们来做一些实际的东西。我们将会在刚才的Help特性前放置AttributeUsage特性以期待在它的帮助下控制Help特性的使用。

using System;
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
this.description = Description_in;
}
protected String description;
public String Description
{
get
{
return this.description;
}
}
}

先让我们来看一下AttributeTargets.Class。它规定了Help特性只能被放在class的前面。这也就意味着下面的代码将会产生错误:

[Help("this is a do-nothing class")]
public class AnyClass
{
[Help("this is a do-nothing method")] //error
public void AnyMethod()
{
}
}

编译器报告错误如下:

AnyClass.cs: Attribute 'Help' is not valid on this declaration type.

It is valid on 'class' declarations only.

我们可以使用AttributeTargets.All来允许Help特性被放置在任何程序实体前。可能的值是:

Assembly,Module,Class,Struct,Enum,Constructor,Method,Property,Field,Event,Interface,Parameter,Delegate

All = Assembly | Module | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Parameter | Delegate

ClassMembers = Class | Struct | Enum | Constructor | Method | Property | Field | Event | Delegate | Interface

下面考虑一下AllowMultiple = false。它规定了特性不能被重复放置多次。

[Help("this is a do-nothing class")]
[Help("it contains a do-nothing method")]
public class AnyClass
{
[Help("this is a do-nothing method")] //error
public void AnyMethod()
{
}
}

它产生了一个编译期错误。

AnyClass.cs: Duplicate 'Help' attribute

Ok,现在我们来讨论一下最后的这个属性。Inherited, 表明当特性被放置在一个基类上时,它能否被派生类所继承。

[Help("BaseClass")]
public class Base
{
}
public class Derive : Base
{
}

这里会有四种可能的组合:

1 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] 
2 [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
3 [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
4 [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]

第一种情况:

如果我们查询(Query)(稍后我们会看到如何在运行期查询一个类的特性)Derive类,我们将会发现Help特性并不存在,因为inherited属性被设置为false。

第二种情况:

和第一种情况相同,因为inherited也被设置为false。

第三种情况:

为了解释第三种和第四种情况,我们先来给派生类添加点代码:

[Help("BaseClass")]
public class Base
{
}
[Help("DeriveClass")]
public class Derive : Base
{
}

现在我们来查询一下Help特性,我们只能得到派生类的属性,因为inherited被设置为true,但是AllowMultiple却被设置为false。因此基类的Help特性被派生类Help特性覆盖了。

第四种情况:

在这里,我们将会发现派生类既有基类的Help特性,也有自己的Help特性,因为AllowMultiple被设置为true。

定义或控制特性的使用AttributeUsage类是另外一个预定义特性类,它帮助我们控制我们自己的定制特性的使用。它描述了一个定制特性如何被使用。

属性和特性的区别可以参考一下: http://developer.51cto.com/art/200908/147097.htm

特性(attribute)的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  3. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  4. 区分元素特性attribute和对象属性property

    × 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...

  5. .Net内置特性Attribute介绍

    特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...

  6. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  7. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. C# 知识特性 Attribute,XMLSerialize,

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...

  10. c#特性attribute:

    特性是被编译到metadata中,  是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...

随机推荐

  1. jQueryValidate的表单提交ajax刷新代码

    $("#form-member-add").validate({ rules:{ username:{ required:true, minlength:2, maxlength: ...

  2. java使用StringBuilder的方法反转字符串输出

    public class FanZhuan { public static void main(String[] args) { /**第一种方法*/ String s = "9876543 ...

  3. 【转载】Spring Cloud全家桶主要组件及简要介绍

    https://blog.csdn.net/xlgen157387/article/details/77773908

  4. 演示stop暴力停止线程导致数据不一致的问题,但是有些有趣的发现 (2017-07-03 21:25)

    如注释所言 /** * Created by weiwei22 on 17/7/3. * * 这里主要是为了演示stop导致的数据不一致的问题.stop会暴力的结束线程并释放锁,所以有可能在恰好写了一 ...

  5. Java集合类框架的最佳实践有哪些?

    1.根据应用需要正确选择要使用的集合类型对性能非常重要,比如:假如知道元素的大小是固定的,那么选用Array类型而不是ArrayList类型更为合适. 2.有些集合类型允许指定初始容量.因此,如果我们 ...

  6. Spring源码学习(2)——默认标签的解析

    上一篇随笔说到Spring对于默认标签和自定义标签的解析方法是不同的,这里详细看一下Spring对于默认标签的解析. private void parseDefaultElement(Element ...

  7. FCC JS基础算法题(1):Factorialize a Number(计算一个整数的阶乘)

    题目描述: 如果用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积.阶乘通常简写成 n!例如: 5! = 1 * 2 * 3 * 4 * 5 = 120. 算法: function fac ...

  8. 剑指Offer 51. 构建乘积数组 (数组)

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  9. Markdown最常用的几个语法

    更多语法请参考:Markdown语法说明 http://wowubuntu.com/markdown/

  10. python中sys和os模块的使用

    在python中,sys,os模块是非常强大的,提供了许多对文件夹.文件和路径的操作方法 sys模块 sys.argv   #命令行执行脚本,其实它就是一个列表 ,sys.argv[0] 是程序自身路 ...