C # attributeusage

Source: Internet
Author: User
    C # attributeusage is very common in our development. So where do we get to know the basic situation of C # attributeusage? This article will introduce you in detail.

     

    C # How is attributeusage used? First, let's take a look at what the attributeusage class is. It is another predefined feature class. The attributeusage class is used to help us control the use of custom features. In fact, the attributeusage class describes a custom feature such as and is used.

    C # attributeusage:

    Attributeusage has three attributes, which can be placed before custom attributes. The first attribute is:

    ◆ Validon

    With this attribute, we can define the program entity before which the custom feature should be placed. All Program entities whose attributes can be placed are listed in attributetargets enumerator. Through the or operation, we can combine several attributetargets values.

    ◆ Allowmultiple
     
    This attribute marks whether our custom features can be repeatedly placed in the same program entity multiple times before.

    ◆ Inherited

    We can use this property to control the inheritance rules of custom features. It indicates whether our features can be inherited.

    C # attributeusage instance:

    Let's do something practical. We will place the attributeusage feature before the help feature to control the use of the help feature with its 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.  }   

Let's take a look at attributetargets. Class. It specifies that the help feature can only be placed before the class. This means that the following code will produce errors:

 
 
  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.  }   

The compiler reports the following errors:

 
 
  1. AnyClass.cs: Attribute 'Help' is not valid on this declaration type.  
  2.  
  3. It is valid on 'class' declarations only.  

Attributetargets. All can be used to allow the help feature to be placed before any program entity. Possible values:

 
 
  1. Assembly,   
  2. Module,   
  3. Class,   
  4. Struct,   
  5. Enum,   
  6. Constructor,   
  7. Method,   
  8. Property,   
  9. Field,   
  10. Event,   
  11. Interface,   
  12. Parameter,   
  13. Delegate,   
  14. All = Assembly | Module | Class |   
  15. Struct | Enum | Constructor |   
  16. Method | Property | Field | Event |   
  17. Interface | Parameter | Delegate,   
  18. ClassMembers = Class | Struct | Enum |  
  19.  Constructor | Method | Property | Field |  
  20.  Event | Delegate | Interface ) 

Consider allowmultiple = false. It specifies that features cannot be repeatedly placed multiple times.

 
 
  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. }  

It produces a compilation error.

 
 
  1. AnyClass.cs: Duplicate 'Help' attribute 

OK. Now let's discuss the final attribute. Inherited indicates whether a feature can be inherited by a derived class when it is placed on a base class.

 
 
  1.  [Help("BaseClass")]   
  2. public class Base   
  3. {   
  4. }   
  5.    
  6. public class Derive : Base   
  7. {   

C # attributeusage has four possible combinations:

 
 
  1. [AttributeUsage(AttributeTargets.Class,  
  2.  AllowMultiple = false, Inherited = false ]   
  3. [AttributeUsage(AttributeTargets.Class,   
  4. AllowMultiple = true, Inherited = false ]   
  5. [AttributeUsage(AttributeTargets.Class,   
  6. AllowMultiple = false, Inherited = true ]   
  7. [AttributeUsage(AttributeTargets.Class,   
  8. AllowMultiple = true, Inherited = true ] 

C # attributeusage:

If we query (query) (we will see how to query the features of a class at runtime) The derive class will find that the help feature does not exist because the inherited attribute is set to false.

C # attributeusage:

This is the same as the first case because inherited is also set to false.

C # attributeusage:

To explain the third and fourth cases, add the vertex code to the derived class:

 
 
  1. [Help("BaseClass")]   
  2. public class Base   
  3. {   
  4. }   
  5. [Help("DeriveClass")]   
  6. public class Derive : Base   
  7. {   

Now let's look at the help feature. We can only get the properties of the derived class, because inherited is set to true, but allowmultiple is set to false. Therefore, the help feature of the base class is overwritten by the help feature of the derived class.

C # attributeusage:

Here, we will find that the derived class has both the help feature of the base class and its own help feature, because allowmultiple is set to true.

C # attributeusage will be introduced here. I hope you can understand and master the use of C # attributeusage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.