Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
当前位置: 主页 > vs2010教程 >

Visual Studio2010讲解C#4.0语法(3)--C#4.0中特性(Attributes)的使用方法

时间:2010-08-06 18:35来源:未知 作者:admin 点击:

Attributes是一种新的描述信息,我们既可以使用attributes来定义设计期信息(例如 帮助文件,文档的URL),还可以用attributes定义运行时信息(例如,使XML中的元素与类的成员字段关联起来)。我们也可以用attributes来创建一个“自描述”的组件。在这篇指南中我们将明白怎么创建属性并将其绑定至各种语言元素上,另外我们怎样在运行时环境下获取到attributes的一些信息。

使用预定义 Attributes

在c#中已有一小组预定义的attributes,在我们学习怎样创建自定义attributes前,先来了解下在我们的代码中使用那些预定义的attributes.
仔细看下该实例,在该实例中我们用到了”Obsolete”attribute,它标记了一个不该再被使用的语言元素(译者注:这里的元素为方法),该属性的第一个参数是string类型,它解释为什么该元素被荒弃,以及我们该使用什么元素来代替它。实际中,我们可以书写任何其它文本来代替这段文本。第二个参数是告诉编译器把依然使用这被标识的元素视为一种错误,这就意味着编译器会因此而产生一个警告。

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( );
    }
}
当我们试图编译上面的上面的程序,我们会得到如下错误:

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

开发自定义Attributes

现在我们即将了解怎么开发自定义的attributes。这儿有个小小处方,有它我们就可以学会创建自定义的attributes。

在C#中,我们的attribute类都派生于System.Attribute类 (A class that derives from the abstract class System.Attribute, whether directly or indirectly, is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration) ,我们就这么行动吧。

using System;

public class

 HelpAttribute : Attribute

{

}
 

注意:按惯例我们是用”Attribute“作为attribute类名的后缀,然而,当我们当我们把attribute绑定到某语言元素时,是不包含“Attribute“后缀的。编译器首先在System.Attribute 的继承类中查找该attribute,如果没有找到,编译器会把“Attribute“追加到该attribute的名字后面,然后查找它。

但是迄今为止,该attribute没有任何用处。为了使它有点用处,让我们在它里面加点东西吧。

using System;
public class HelpAttribute : Attribute
{
    public HelpAttribute(String Descrition_in)
    {
        this.description = Description_in;
    }
    protected String description;
    public String Description
    {
        get
        {
            return this.description;
                
        }           
    }   
}
[Help("this is a do-nothing class")]
public class AnyClass
{
}

在上面的例子中,我们在attribute类中添加了一个属性,在最后一节中,我们将在运行时查询该属性。 AttributeTargets.Class. 它规定这个help attribute 只能放置在语言元素”class”之上。这就意味着,下面的代码将会产生一个错误。

定义或控制自定义Attribute的用法

AttributeUsage 类是另一预定义类(译者注:attribute类本身用这个atrribute System.AttributeUsage来标记),它将帮助我们控制我们自定义attribute的用法,这就是,我们能为自定义的attribute类定义attributes。

它描述了一个自定义attribute类能被怎样使用。

AttributeUsage 提供三个属性,我们能将它们放置到我们的自定义attribute类上, 第一个特性是:

ValidOn

通过这个属性,我们能指定我们的自定义attribute可以放置在哪些语言元素之上。这组我们能把自定义attribute类放置其上的语言元素被放在枚举器AttributeTargets 中。我们可以使用bitwise(译者注:这个词不知道怎么翻译好,但他的意思是可以这么用:[AttributeUsage((AttributeTargets)4, AllowMultiple = false, Inherited = false )],4代表就是“class”元素,其它诸如1代表“assembly”,16383代表“all”等等)或者”.”操做符绑定几个AttributeTargets 值。(译者注:默认值为AttributeTargets.All)

AllowMultiple

该属性标识我们的自定义attribte能在同一语言元素上使用多次。(译者注:该属性为bool类型,默认值为false,意思就是该自定义attribute在同一语言元素上只能使用一次)

Inherited

我们可以使用该属性来控制我们的自定义attribute类的继承规则。该属性标识我们的自定义attribute是否可以由派生类继承。((译者注:该属性为bool类型,默认值为false,意思是不能继承)

让我们来做点实际的东西吧,我们将把AttributeUsage attribute 放置在我们的help attribute 上并在它的帮助下,我们来控制help attribute的用法。

 

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.All 来允许 Help attribute 可以放置在任何预定义的语言元素上,那些可能的语言元素如下

[Help("this is a do-nothing class")]

public class AnyClass

{

    [Help("this is a do-nothing method")]    //error

    public void AnyMethod()

    {

    }


我们可以使用

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. 这就规定该 attribute 不能在同一语言元素上放置多次.

[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()

    {

    }

}

(责任编辑:admin)

Tags:vs2010 C#4.0
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价