C # Attribute)

Source: Internet
Author: User
Attributes is a brand new declarative information. We can not only define design-level information (such as help file and URL for documentation) through features, but also run-time information (such as associating XML with class ), we can also use features to build self-describing components. In this tutorial, we will see how to create and add features to various Program Entity and how to obtain feature information in the runtime environment. Definition as described in msdn ----- "a feature is an additional declarative information that is assigned to a declaration ." The pre-defined feature has a small set of pre-defined features in C. Before learning how to create custom features (custom attributes), let's take a look at our Code How to use predefined features. Using system; public class anyclass {[obsolete ("Don't use old method, use new method", true)] Static void old () {} static void new () {} public static void main () {old () ;}} let's take a look at the above example. In this example, we use the obsolete feature, it marks a program entity that should not be used again. The first parameter is a string that explains why the object is outdated and what entity should be used to replace it. Actually, you can write any text here. The second parameter tells the compiler to treat the obsolete program entity as an error. The default value is false, which means the compiler generates a warning. When we try to compile the above program, we will get an error: anyclass. old () 'is obsolete: 'don' t use old method, use new method' custom feature (M attributes) Now let's see how to develop our own features. First, we need to derive our own feature class from system. Attribute (a class inherited from the system. Attribute abstract class, whether directly or indirectly inherited, will become a feature class. The declaration of a feature class defines a new feature that can be placed on the Declaration ). Using system; public class helpattribute: attribute {} whether you believe it or not, we have established a custom feature, now we can use it to decorate the existing class, just as we used the obsolete attribute above. [Help ()] public class anyclass {} Note: using the attribute suffix for a feature class name is a convention. However, when we add features to a program entity, whether or not to include attribute suffixes is our freedom. The compiler first looks for the added feature class in the derived class of system. attribute. If not found, the compiler adds the attribute suffix to continue searching. So far, this feature has not played any role. Next we will add something to make it more useful. Using system; public class helpattribute: attribute {public helpattribute (string descrition_in) {This. description = description_in;} protected string description; Public String description {get {return this. description ;}} [help ("This is a do-nothing class")] public class anyclass {}in the preceding example, we added an attribute to the helpattribute feature class and we will search for it in the runtime environment in the subsequent sections. The attributeusage class defines or controls the use of features. It 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 which 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. The allowmultiple attribute indicates whether our custom features can be repeatedly placed in the same program entity multiple times. 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 helpattriple: 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 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 following error: anyclass. CS: attribute 'help' is not valid on this declaration type. it is valid on 'class' declarations only. we can use attributetargets. all 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: [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, inher Ited = true] First case: If we query (query) (later 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. The second case is the same as the first case because inherited is also set to false. Case 3: To explain the case 3 and case 4, add the following 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 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.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.