当前位置: 移动技术网 > IT编程>开发语言>.net > C#特性详解

C#特性详解

2018年09月17日  | 移动技术网IT编程  | 我要评论

津元参价格,氯丁胶,亡国的阿基德

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

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

     

      

 1 using system;
 2 public class anyclass
 3 {
 4     [obsolete("don't use old method, use new method", true)]
 5     static void old( ) { } 
 6     static void new( ) { }
 7     public static void main( )
 8     {
 9         old( );
10     }
11 }

 

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

  当我们尝试编译上面这段程序的时候,我们将会得到一个错误: 
  anyclass.old()' is obsolete: 'don't use old method, use new method' 


  开发定制特性(custom attributes) 

  现在让我们来看看如何开发我们自己的特性。 
  首先我们要从system.attribute派生出我们自己的特性类(一个从system.attribute抽象类继承而来的类,不管是直接还是间接继承,都会成为一个特性类。特性类的声明定义了一种可以被放置在声明之上新的特性)。

  1 using system; 2 public class helpattribute : attribute 3 { 4 } 

  不管你是否相信,我们已经建立了一个定制特性,现在我们可以用它来装饰现有的类就好像上面我们使用obsolete attribute一样。

 

  

1 [help()]
2 public class anyclass
3 {
4 }

 

  注意:对一个特性类名使用attribute后缀是一个惯例。然而,当我们把特性添加到一个程序实体,是否包括 attribute后缀是我们的自由。编译器会首先在system.attribute的派生类中查找被添加的特性类。如果没有找到,那么编译器会添加 attribute后缀继续查找。 

    到目前为止,这个特性还没有起到什么作用。下面我们来添加些东西给它使它更有用些。

 

 1 using system;
 2 public class helpattribute : attribute
 3 {
 4     public helpattribute(string descrition_in)
 5     {
 6         this.description = description_in;
 7     }
 8     protected string description;
 9     public string description
10     {
11         get
12         {
13             return this.description;
14         }
15     }
16 }
17 [help("this is a do-nothing class")]
18 public class anyclass
19 {
20 }

 

    在上面的例子中,我们给helpattribute特性类添加了一个属性并且在后续的部分中我们会在运行时环境中查寻它。 

  定义或控制特性的使用 

  attributeusage类是另外一个预定义特性类,它帮助我们控制我们自己的定制特性的使用。它描述了一个定制特性如和被使用。 
  attributeusage有三个属性,我们可以把它放置在定制属性前面。第一个属性是: 


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


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

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

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

 

 1 using system;
 2 [attributeusage(attributetargets.class), allowmultiple = false,
 3 inherited = false ]
 4 public class helpattribute : attribute
 5 {
 6     public helpattribute(string description_in)
 7     {
 8         this.description = description_in;
 9     }
10     protected string description;
11     public string description
12     {
13         get
14         {
15             return this.description;
16         }
17     }
18 }

 

 

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

 

  

1 [help("this is a do-nothing class")]
2 public class anyclass
3 {
4     [help("this is a do-nothing method")] //error
5     public void anymethod()
6     {
7     }
8 }

 

  编译器报告错误如下:
  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。它规定了特性不能被重复放置多次。

 

1 [help("this is a do-nothing class")]
2 [help("it contains a do-nothing method")]
3 public class anyclass
4 {
5     [help("this is a do-nothing method")] //error
6     public void anymethod()
7     {
8     }
9 }

 

  它产生了一个编译期错误。 
  anyclass.cs: duplicate 'help' attribute 

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

 

1 [help("baseclass")]
2 public class base
3 {
4 }
5 public class derive : base
6 {
7 }

 

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

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。 

  第三种情况: 

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

 

1 [help("baseclass")]
2 public class base
3 {
4 }
5 [help("deriveclass")]
6 public class derive : base
7 {
8 }

 

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

  第四种情况: 

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

 

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

 

属性和特性的区别可以参考一下: 

本文来自csdn博客,转载请标明出处:

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网