The attributeusage class is another predefined feature 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, which can be placed before custom attributes. The first attribute is:
Validon
Through this attribute, we can define what the custom feature should be Program Place the object before it. 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.
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 An error occurs:
[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
{
}
There are four possible combinations:
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.
Case 2:
This is the same as the first case because inherited is also set to false.
Case 3:
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.
Case 4:
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.
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