Attributes是一种新的描述信息,我们既可以使用attributes来定义设计期信息(例如 帮助文件,文档的URL),还可以用attributes定义运行时信息(例如,使XML中的元素与类的成员字段关联起来)。我们也可以用attributes来创建一个“自描述”的组件。在这篇指南中我们将明白怎么创建属性并将其绑定至各种语言元素上,另外我们怎样在运行时环境下获取到attributes的一些信息。
使用预定义 Attributes
在c#中已有一小组预定义的attributes,在我们学习怎样创建自定义attributes前,先来了解下在我们的代码中使用那些预定义的attributes.
仔细看下该实例,在该实例中我们用到了”Obsolete”attribute,它标记了一个不该再被使用的语言元素(译者注:这里的元素为方法),该属性的第一个参数是string类型,它解释为什么该元素被荒弃,以及我们该使用什么元素来代替它。实际中,我们可以书写任何其它文本来代替这段文本。第二个参数是告诉编译器把依然使用这被标识的元素视为一种错误,这就意味着编译器会因此而产生一个警告。
usingSystem;
publicclassAnyClass
{
[Obsolete("Don'tuseOldmethod,useNewmethod",true)]
staticvoidOld(){}
staticvoidNew(){}
publicstaticvoidMain()
{
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) ,我们就这么行动吧。
usingSystem;
publicclassHelpAttribute:Attribute
{
}
不管你是否相信我,就这样我们就已经创建了一个自定义
attribute。现在就可以用它来装饰我们的类了,就像我们使用obsolete attribute一样。
[Help()]
publicclassAnyClass
{
}
注意:按惯例我们是用”Attribute“作为attribute类名的后缀,然而,当我们当我们把attribute绑定到某语言元素时,是不包含“Attribute“后缀的。编译器首先在System.Attribute 的继承类中查找该attribute,如果没有找到,编译器会把“Attribute“追加到该attribute的名字后面,然后查找它。
但是迄今为止,该attribute没有任何用处。为了使它有点用处,让我们在它里面加点东西吧。
usingSystem;
publicclassHelpAttribute:Attribute
{
publicHelpAttribute(StringDescrition_in)
{
this.description=Description_in;
}
protectedStringdescription;
publicStringDescription
{
get
{
returnthis.description;
}
}
}
[Help("thisisado-nothingclass")]
publicclassAnyClass
{
}
在上面的例子中,我们在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的用法。
usingSystem;
[AttributeUsage(AttributeTargets.Class,AllowMultiple=false,Inherited=false)]
publicclassHelpAttribute:Attribute
{
publicHelpAttribute(StringDescription_in)
{
this.description=Description_in;
}
protectedStringdescription;
publicStringDescription
{
get
{
returnthis.description;
}
}
}
首先我们注意
AnyClass.cs: Attribute 'Help' is not valid on this declaration type.
It is valid on 'class' declarations only.
现在试着把它绑定到方法。 AttributeTargets.All 来允许 Help attribute 可以放置在任何预定义的语言元素上,那些可能的语言元素如下:
[Help("thisisado-nothingclass")]
publicclassAnyClass
{
[Help("thisisado-nothingmethod")]//error
publicvoidAnyMethod()
{
}
}
我们可以使用
-
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("thisisado-nothingclass")]
[Help("itcontainsado-nothingmethod")]
publicclassAnyClass
{
[Help("thisisado-nothingmethod")]//error
publicvoidAnyMethod()
{
}
}
下面我给出在VS2010中操作Attributes的综合性实例:
首先打开Visual Studio2010创建一个基于C#的ConsoleApplication工程Attributes:
在Program.cs文件里写入如下代码:
按下F5开始调试,运行界面如下:
分享到:
相关推荐
Essential C# 4.0 完美版 附件里有两个pdf,内容完全一样,似乎清晰度不一样 984 pages Publisher: Addison-Wesley Professional; 3 edition (March 20, 2010) Language: English ISBN-10: 0321694694 ISBN-13:...
Essential C# 4.0 is a well-organized,“no-fluff” guide to all versions of C# for programmers at all levels of C# experience. This fully updated edition shows how to make the most of C# 4.0’s new ...
该书详细介绍C#在.Net Framework 4.0中的应用,介绍C# 4.0新特征,适合.Net开发人员作为参考书使用。 Following an introduction to C#, readers learn about Best practices for object-oriented programming in ...
Essential C# 4.0, 3rd Edition (Microsoft .NET Development Series) 984 pages Publisher: Addison-Wesley Professional; 3 edition (March 20, 2010) Language: English ISBN-10: 0321694694 ISBN-13: 978...
中文名: C#4.0编程 (第6版,涵盖Visual Studio 2010以及.NET4) 原名: Programming C# 4.0: Building Windows, Web, and RIA Applications for the .NET 4.0 Framework 作者: Ian Griffiths Matthew Adams Jesse ...
Effective C# (Covers C# 4.0): 50 Specific Ways to Improve Your C#, Second Edition 352 pages Publisher: Addison-Wesley Professional; 2 edition (March 15, 2010) Language: English ISBN-10: 0321658701...
JavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-attributes-apiJavaEE源代码 commons-...
commons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zipcommons-attributes-compiler-2.2.zip
commons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zipcommons-attributes-2.2.zip
commons-attributes-api.jar,commons-attributes-compiler.jar,commons-beanutils.jar,commons-codec.jar,commons-collections.jar,commons-dbcp.jar,commons-digester.jar,commons-discovery.jar,commons-...
commons-attributes-api-2.2.jar。学习资源,
介绍了Adaptive Color Attributes for Real-Time Visual Tracking,CVPR2014,文章中算法的基本流程,以及优化的重点
commons-attributes-api-2.2.zipcommons-attributes-api-2.2.zipcommons-attributes-api-2.2.zipcommons-attributes-api-2.2.zipcommons-attributes-api-2.2.zipcommons-attributes-api-2.2.zip
AnonymousDelegates,Arrays,Attributes,CollectionClasses,COMInteropPart1,Generics等示例
commons-attributes-api.jar
JavaEE源代码 commons-attributes-compilerJavaEE源代码 commons-attributes-compilerJavaEE源代码 commons-attributes-compilerJavaEE源代码 commons-attributes-compilerJavaEE源代码 commons-attributes-...