How is C# AttributeUsage used? First, let's understand what the AttributeUsage class is. It is another predefined feature class. The function of the AttributeUsage class is to help us control the use of customized features. In fact, the AttributeUsage class describes a custom feature such as and is used.
You need to understand the use of C# AttributeUsage:
AttributeUsage has three properties, which we can place before the custom properties.
- ValidOn
Through this property, we can define which program entity the customization property should be placed in front of. All program entities that can be placed in the AttributeTargets enumerator. Through the OR operation, we can combine several AttributeTargets values. - AllowMultiple
This property marks whether our custom features are repeatedly placed before the same program entity. - Inherited
We can use this property to control the inheritance rules of custom features. It marks whether our characteristics are inherited.
Example of C# AttributeUsage:
Let's do something practical. We will place the AttributeUsage feature before the Help feature in order to control the use of the Help feature with its help.
using System; [AttributeUsage(), AllowMultiple = false, Inherited = false ] public class HelpAttribute : Attribute { public HelpAttribute(String Description_in) { = Description_in; } protected String description; public String Description { get { return ; } } }
Let's take a look first. It stipulates that Help feature can only be placed in front of the class. This means that the following code will produce an error:
[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 error as follows:
: Attribute ‘Help' is not valid on this declaration type.
It is valid on ‘class' declarations only.
We can use to allow Help features to be placed in front of any program entity. Possible values are:
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 )
Let's consider AllowMultiple = false. It specifies that the characteristics 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 generates a compile-time error.
: Duplicate ‘Help' attribute
Ok, let’s discuss the last attribute now. 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 { }
There are four possible combinations of the use of C# AttributeUsage:
[Help("BaseClass")] public class Base { } public class Derive : Base { }
The first case of using C# AttributeUsage:
If we query (we will see how to query a class's properties during runtime) Derive class, we will find that the Help property does not exist because the inherited property is set to false.
The second case of using C# AttributeUsage:
Same as the first case, since inherited is also set to false.
The third situation of using C# AttributeUsage:
To explain the third and fourth cases, we first add point code to the derived class:
[Help("BaseClass")] public class Base { } [Help("DeriveClass")] public class Derive : Base { }
Now let's query the Help feature, we can only get the attributes 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 overridden by the Help feature of the derived class.
The fourth case of using C# AttributeUsage:
Here, we will find that derived classes have both the Help characteristics of the base class and their own Help characteristics, because AllowMultiple is set to true.
This is all for you to introduce the relevant content of C# AttributeUsage. I hope it will be helpful for you to understand and master the use of C# AttributeUsage.
This is the end of this article about the detailed explanation of C# AttributeUsage usage cases. For more related content on C# AttributeUsage usage, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!