The AttributeUsage class is another predefined attribute class that helps us control the use of our own custom features. It describes a custom feature such as and is used.
AttributeUsage has three attributes that we can place in front of the custom attribute. The first property is:
Validon
With this attribute, we can define which program entity the custom attribute should be placed in front of. All program entities that can be placed in a property are listed in AttributeTargets Enumerator. We can combine several AttributeTargets values by using the OR operation.
AllowMultiple
This property marks whether our custom attributes can be repeatedly placed in front of the same program entity.
Inherited
We can use this property to control the inheritance rules for custom attributes. It marks the ability of our attributes to be inherited.
Now let's do something practical. We will place the AttributeUsage feature in front of the help feature to look forward to controlling the use of the aid feature with its assistance.
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 first. It stipulates that the Help attribute can only be placed in front of the class. This also means that the following code will produce an error:
[Help (' This is a do-nothing class ')]
public class Anyclass
{
[Help (' This is ' do-nothing method ')]//error
public void Anymethod ()
{
}
}
The compiler reported an error as follows:
AnyClass.cs:Attribute ' help ' isn't valid on this declaration type.
It is valid in ' class ' declarations only.
We can use AttributeTargets.All to allow the Help attribute to be placed in front of any program entity. The 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)
Consider AllowMultiple = False below. It stipulates that the attribute 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 ' do-nothing method ')]//error
public void Anymethod ()
{
}
}
It produces a compile-time error.
AnyClass.cs:Duplicate ' help ' attribute
Ok, now let's talk about this last attribute. Inherited, which indicates whether it can be inherited by derived classes when the attribute is placed on a base class.
[Help ("BaseClass")]
public class Base
{
}
public class Derive:base
{
}
There are four possible combinations:
If we query (query) (which we will see later on how the attributes of a class are queried at runtime) derive class, we will find that the Help attribute does not exist because the inherited property is set to False.
In the second case:
The same as the first case, because the inherited is also set to false.
In the third case:
To explain the third and fourth cases, let's add a point code to the derived class:
[Help ("BaseClass")]
public class Base
{
}
[Help ("Deriveclass")]
public class Derive:base
{
}
Now we're going to 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. The help attribute of the base class is therefore overwritten by the derived class help attribute.
The fourth situation:
Here we will find that derived classes have both the Help attribute of the base class and their help attribute because the AllowMultiple is set to true.
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