C#中的Attribute定义及用法

1.Attribute定义

公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据(metadata)保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。 我们简单的总结为:定制特性attribute,本质上是一个类,其为目标元素提供关联附加信息,并在运行期以反射的方式来获取附加信息。
 
 
 
2.Attribute的长记法和短记法
说明ConditionalAttribute和Conditional是一样 的
  1. // 长记法
  2. [ConditionalAttribute("LI")]
  3. [ConditionalAttribute("NoBug")]
  4. publicstaticvoidFun()
  5. {Console.WriteLine("Created By Li, NoBug.");}
  6. // 短记法
  7. [Conditional("LI")]
  8. [Conditional("NoBug")]
  9. publicstaticvoidFun()
  10. {Console.WriteLine("Created By Li, NoBug.");}
 
 3.预处理指令
#define  debug
虽然选择的是release模式,但是在代码中预定义是debug模式,所以还是代码还是会按照debug模式来执行的
 
4.常用指令
[Conditional("DEBUG)]     下面的代码在DEBUG模式下,才会被执行,注意里面的DEBUG必须大写,小写就变成预定义指令了
[obsolete   ("这段不会执行"),true] 弃用下面的代码段,true代表代码如果执行了,就会抛出一个异常,现实“这段代码不会执行”
[AttributeUsage(AttributeTargets.Class,AllowMultiple=false,inherited = false)]
AttributeUsage确定可以如何使用自定义特性类。   AttributeUsage 是一个可应用于自定义特性定义,以便控制如何应用新特性的特性。   显式应用时默认设置如下所示,第一个属性规定自定义属性可以用在哪些地方,第二属性规定是否可以多用,第三个属性规定自定义属性是否可以继承
 
 
attribute 的实例化

就像牡蛎天生就要吸附在礁石或船底上一样,Attribute 的实例一构造出来就必需“粘”在一个什么目标上。

  Attribute 实例化的语法是相当怪异的,主要体现在以下三点:

  1.  不使用new 操作符来产生实例,而是使用在方括号里调用构造函数来产生实例。

  2.  方括号必需紧挨着放置在被附着目标的前面。

  3.  因为方括号里空间有限,不能像使用new 那样先构造对象,然后再给对象的属性(Property)赋值。

  因此,对Attribute 实例的属性赋值也在构造函数的圆括号里。

  并且,Attribute 实例化时尤其要注意的是:

  1.  构造函数的参数是一定要写。有几个就得写几个,因为你不写的话实例就无法构造出来。

  2.  构造函数参数的顺序不能错。调用任何函数都不能改变参数的顺序,除非它有相应的重载(Overload)。因为这个顺序是固定的,有些书里称其为“定位参数”(意即“个数和位置固定的参数”)。

  3. 对Attribute 实例的属性的赋值可有可无。反正它会有一个默认值,并且属性赋值的顺序不受限制。有些书里称属性赋值的参数为“具名参数”。

 

自定义Attribute 实例

 

 在此,我们不使用.NET  Framework 中的各种Attribute 系统特性,而是从头自定义一个全新的Attribute 类。

  示例代码如下:

 

  1. using System;
  2. namespace Con_Attribute
  3. {
  4. classProgram3
  5. {
  6. staticvoidMain(string[] args)
  7. {
  8. //使用反射读取Attribute
  9. System.Reflection.MemberInfo info =typeof(Student);//通过反射得到Student类的信息
  10. Hobby hobbyAttr =(Hobby)Attribute.GetCustomAttribute(info,typeof(Hobby));
  11. if(hobbyAttr !=null)
  12. {
  13. Console.WriteLine("类名:{0}", info.Name);
  14. Console.WriteLine("兴趣类型:{0}", hobbyAttr.Type);
  15. Console.WriteLine("兴趣指数:{0}", hobbyAttr.Level);
  16. }
  17. }
  18. }
  19. //注意:"Sports" 是给构造函数的赋值, Level = 5 是给属性的赋值。
  20. [Hobby("Sports",Level=5)]
  21. classStudent
  22. {
  23. [Hobby("Football")]
  24. publicstring profession;
  25. publicstringProfession
  26. {
  27. get {return profession;}
  28. set{ profession = value;}
  29. }
  30. }
  31. //建议取名:HobbyAttribute
  32. classHobby:Attribute// 必须以System.Attribute 类为基类,规范的写法一般是HobbyAttribute,用的时候可以简写为[Hobby(“”)]
  33. {
  34. // 参数值为null的string 危险,所以必需在构造函数中赋值
  35. publicHobby(string _type)// 定位参数
  36. {
  37. this.type = _type;
  38. }
  39. //兴趣类型
  40. privatestring type;
  41. publicstringType
  42. {
  43. get {return type;}
  44. set{ type = value;}
  45. }
  46. //兴趣指数
  47. privateint level;
  48. publicintLevel
  49. {
  50. get {return level;}
  51. set{ level = value;}
  52. }
  53. }
  54. }
5.通过反射来捕捉属性的值
首先自定义一个Person类
  1. classPerson:Attribute{
  2. privatestring _name;
  3. privateint _age;
  4. publicstringName{
  5. set{this._name = value;}
  6. get {returnthis._name;}
  7. }
  8. publicintAge{
  9. set{this._age = value;}
  10. get {returnthis._age;}
  11. }
  12. publicPerson(string s,int i){
  13. this.Name= s;
  14. this.Age= i;
  15. }
  16. }
再在使用一个类的时候加上这个属性
  1. [Person("spike",23)]
  2. class test {
  3. privateint aa;
  4. publicstring ss;
  5. }
再在Main函数中使用type类型的的GetCustomAttributes方法来返回属性的类,达到遍历属性类的目的
  1. Person per;
  2. foreach(var item intypeof(test).GetCustomAttributes(true)){
  3. per = item asPerson;//若item是自定义的Person类,则将Person类返回
  4. if(per !=null){
  5. Console.WriteLine(per.Name);
  6. Console.WriteLine(per.Age);
  7. }
  8. }
  9. Console.ReadKey();

 



转载于:https://www.cnblogs.com/weloveshare/p/5294812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值