Attribute的应用
Attribute示例:
1.自定义Attribute类:VersionAttribute
//使用限制标志,这里限定只能把标签打在class上 [AttributeUsage(AttributeTargets.Class)] //自定义的Attribute必须直接或者间接继承System.Attribute public class VersionAttribute : Attribute { public string Name { get; set; } public DateTime Date { get; set; } public string Describtion { get; set; } }
2.使用自定义Attribute的Class:
[Version(Name = "itbbb", Date = DateTime.Now, Describtion = "itbbb.com")] public class ITbbb { //... }
3.上面这个Class中的Attribute一般会被如何使用呢
class Program { static void Main(string[] args) { var info = typeof(ITbbb); var classAttribute = (VersionAttribute) Attribute.GetCustomAttribute(info, typeof(VersionAttribute)); Console.WriteLine(classAttribute.Name); Console.WriteLine(classAttribute.Date); Console.WriteLine(classAttribute.Describtion); } }
Attribute定义:
公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据(metadata)保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。
我简单的总结为:定制特性attribute,本质上是一个类,其为目标元素提供关联附加信息,并在运行期以反射的方式来获取附加信息。
上面代码中“[AttributeUsage(AttributeTargets.Class)] ”说明了Attribute提供附加信息的元素是Class,所以如果上面第二段的代码改为:
public class ITbbb { [Version(Name = "baby", Date = DateTime.Now, Describtion = "it's class")] public void Test() { } }
编译错误,因为它不允许标记在方法上,AttributeTargets的枚举在下面会给大家列出。
attribute类是在编译的时候被实例化的,而不是像通常的类那样在运行时候才实例化。所以在第三段代码中,你可以在没有实例化ITbbb对象的情况下取到它的Attribute信息。由于Attribute类是在编译的时候被实例化的,所以你还可以用外部工具维护这些Attribute信息。
实现Attribute时需要注意的一些问题
1.自定义的Attribute必须直接或者间接继承System.Attribute。
2.这里有一个约定:所有自定义的特性名称都应该有个Attribute后缀。因为当你的Attribute施加到一个程序的元素上的时候,编译器先查找你的Attribute的定义,如果没有找到,那么它就会查找“Attribute名称”+Attribute的定义。如果都没有找到,那么编译器就报错。这就是为什么我可以再上面第一段代码中,定义一个VersionAttribute,但在第二段代码中,我使用却是Version这个Attribute。
Attribute可以关联的元素包括:
程序集(assembly)、模块(module)、类型(type)、属性(property)、事件(event)、字段(field)、方法(method)、参数(param)、返回值(return)。
AttributeTargets目标包括:
标记 | 说明 |
All | 可以对任何应用程序元素应用属性。 |
Assembly | 可以对程序集应用属性。 |
Class | 可以对类应用属性。 |
Constructor | 可以对构造函数应用属性。 |
Delegate | 可以对委托应用属性。 |
Enum | 可以对枚举应用属性。 |
Event | 可以对事件应用属性。 |
Field | 可以对字段应用属性。 |
GenericParameter | 可以对泛型参数应用属性。 |
Interface | 可以对接口应用属性。 |
Method | 可以对方法应用属性。 |
Module | Module 指的是可移植的可执行文件(.dll 或 .exe),而非 Visual Basic 标准模块。 |
Parameter | 可以对参数应用属性。 |
Property | 可以对属性 (Property) 应用属性 (Attribute)。 |
ReturnValue | 可以对返回值应用属性。 |
Struct | 可以对结构应用属性,即值类型。 |
AttributeUsageAttribute中的3个属性(Property)说明:
属性名 | 说明 |
ValidOn | 该定位参数指定可在其上放置所指示的属性
http://celebrexonline-pharmacy.com/http://lipitorgeneric-online247.com/vytorin-cholesterol-meds.htmlhttp://nexiumonline-generic.com/catalog/Gastrointestinal/Prilosec.htmnexium patent expirationviagra onlinehttp://lexapropharmacy-generic.com/catalog/Depression/Clozaril.htmcialis generic
(Attribute) 的程序元素。AttributeTargets 枚举数中列出了可在其上放置属性 (Attribute) 的所有可能元素的集合。可通过按位“或”运算组合多个 AttributeTargets 值,以获取所需的有效程序元素组合。 |
AllowMultiple | 该命名参数指定能否为给定的程序元素多次指定所指示的属性。 |
Inherited | 该命名参数指定所指示的属性能否由派生类和重写成员继承。 |
–来自北北整理,相关文章请学习《C#中Property和Attribute的区别》
--转载请注明: IT圈子,ITBBB.COM » Attribute的应用