Use of definition or control characteristics
The AttributeUSAGE class is another predefined feature class that helps us control our own customizable features. It describes a customization feature such as and is used.
AttributeUSage has three properties, we can place it in front of custom properties. The first attribute is:
Validon
With this property, we can define the customization characteristics to place before the program entity is placed. An attribute can be placed all program entities listed in AttributeTargets Enumerator. Through or operate we can combine several AttributeTargets values.
Allowmultiple
This property marks whether our customization characteristics can be repeatedly placed multiple times before the same program entity.
Inherited
We can use this attribute to control the inheritance rules for customization characteristics. It marked whether our feature can be inherited.
Let us do something practical. We will place AttributeUSAGE feature prior to the Help feature to monitor the use of HELP characteristics 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, 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 in front of the Class. This means that the following code will generate errors:
[Help ("this is a do-nothing class"]
Public class anyclass
{
[Help ("this is a do-nothing method"] // error
Public void Anymethod ()
{
}
}
The compiler report is wrong as follows:
Anyclass.cs: attribute 'help' is not valid on this deflaration type.
IT Is Valid on 'Class' Declarations Only.
We can use AttributeTargets.all to allow HELP characteristics to be placed before 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 | Construction | Method | Property | Field | Event | Interface | Parameter | Delegate,
Classmembers = Class | Struct | ENUM | Constructor | Method | Property | Field | Event | DELEGATE | Interface)
Let's take into account the allowmultiple = false. It specifies that the characteristics cannot be repeated 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 compile period error.
Anyclass.cs: duplicate 'Help' Attribute
OK, now let's discuss the last attribute. Inherited, indicating whether it can be inherited by the derived class when the feature is placed on a base class.
[Help ("Baseclass")]
Public Class Base
{
}
Public Class Derive: Base
{
}
There will be four possible combinations here:
[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]
The first case:
If we query (Query) (later we will see how to query a class in the runtime) Derive class, we will find that the HELP feature does not exist because the inherited property is set to false.
The second case:
As in the first case, because inherited is also set to false.
The third case:
In order to explain the third and fourth cases, let's add some code to the derived class first:
[Help ("Baseclass")]
Public Class Base
{
}
[Help ("DeriveClass")]]]
Public Class Derive: Base
{
}
Now let's query the HELP feature, we can only get the attribute of derived class, because inherited is set to True, but the allowmultiple is set to false. Therefore, the HELP characteristic of the base class is covered by the derived HELP characteristics.
The fourth case:
Here, we will find that the derived class has both the Kelp characteristics of the base class, and has its own HELP characteristics because AllowMultiPle is set to True.