C # properties (Attribute) entry (two) Author: cppbug (translation) www.ASPCool.com time: 2004-4-2 21:05:05 Views: 5666
Using the Attributeusage class for definition or control features is another predefined feature class, which helps us control our own customizable use. 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 passes this property, we can define what program entities should be placed before the customization feature. An attribute can be placed all program entities listed in AttributeTargets Enumerator. Through or operate we can combine several AttributeTargets values. The baseowmultiple is tagged to whether our customization feature 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 {get {return 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 () {}} compiler reports the error 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 features 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 | Constructor | Method | Property | Field | Event | Interface | Parameter | DELEGATE, CLASSMBERS = Class | Struct | Enum | Construction | Method | Property | Field | Event | Delegate | Interface Let's take into account soMultiPle = 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 {} Here 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, Inherited = true] The first case: If we query (query) (We will see how to query a class at runtime) Derive class, we will find that the HELP feature does not exist because the inherited property is set to false. The second case: and the first case, Because inherited is also set to false.