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.
- using System;
- [AttributeUsage(AttributeTargets.Class), AllowMultiple = false,
- Inherited = false ]
- public class HelpAttribute : Attribute
- {
- public HelpAttribute(String Description_in)
- {
- this.description = Description_in;
- }
- protected String description;
- public String Description
- {
- get
- {
- return this.description;
- }
- }
- }
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:
- [Help("this is a do-nothing class")]
- public class AnyClass
- {
- [Help("this is a do-nothing method")] //error
- public void AnyMethod()
- {
- }
- }
The compiler reports the following errors:
- AnyClass.cs: Attribute 'Help' is not valid on this declaration type.
-
- 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:
- 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 )
Consider allowmultiple = false. It specifies that features cannot be repeatedly placed multiple times.
- [Help("this is a do-nothing class")]
- [Help("it contains a do-nothing method")]
- public class AnyClass
- {
- [Help("this is a do-nothing method")] //error
- public void AnyMethod()
- {
- }
- }
It produces a compilation error.
- 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.
- [Help("BaseClass")]
- public class Base
- {
- }
-
- public class Derive : Base
- {
- }
C # attributeusage has four possible combinations:
- [AttributeUsage(AttributeTargets.Class,
- AllowMultiple = false, Inherited = false ]
- [AttributeUsage(AttributeTargets.Class,
- AllowMultiple = true, Inherited = false ]
- [AttributeUsage(AttributeTargets.Class,
- AllowMultiple = false, Inherited = true ]
- [AttributeUsage(AttributeTargets.Class,
- 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:
- [Help("BaseClass")]
- public class Base
- {
- }
- [Help("DeriveClass")]
- public class Derive : Base
- {
- }
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.