C # language series lecture (15) feature

xiaoxiao2021-03-06  75

Characteristics (attribute) is an exciting innovation introduced by C # to component programming, which allows us to provide additional descriptive information for various elements of the program, such as structures, interfaces, methods, etc., these descriptive information The program code can be extracted when running. See the following example of a program: using System; public class AuthorAttribute: Attribute {// OF feature class public AuthorAttribute (string name) {this.name = name;} public string Name {get {return name;}} private string name;} [AUTHOR ("Anders Hejlsberg")] // Feature instantiate class myclass {int count; public myclass (int count) {this.count = count;} public int count {ras}} class test;}}} class test {static Void main () {type t1 = typeof (myclass); type t2 = typeof (authorattribute); Object [] arr = t1.getCustomAttributes (T2, true); // Get Features Instance (Array) Console.write ("The Author Of myclass is: "); Foreach (Object O in Arr) {AuthorAttribute AA = (AuthorAttribute) O; console.write (" {0} ", aa.name);} console.writeline ();}}} program first declares And implement an AuthorAttribute feature class, and the feature class must directly or indirectly inherit the abstract class System.attribute, which is often the first step of using the feature, which will provide the data structure of the descriptive information. It is worth noting that the suffix "attribute" in the AuthorAttribut feature class is the characteristic class name method recommended by C #, which is not necessary. When implementing the class myclass, the AuthorAttribute feature class is used to provide the information information for MyClass. Features Instantiation Heethers AUTHOR ("Anders Hejlsberg") Help Complete this line, parentheses "[]" tells C # in progressive instantiation. Authoraattribute class Remove the suffix "Attribute" - C # compiler can be automatically identified, of course, after replacement of AuthorAttribute, the effect is the same, where ("Anders Hejlsberg" will call AuthorAttribute (String) Name) Example constructor. After the statement is instantiated by such a feature, a "authorattribute" is provided for the "AuthorAttribute). In the Test test class, we can get our previously set description information by mapping. The running program can get the following result: The author of myclass is: Anders Hejlsberg. There are two parameters when the parameter feature is instantiated, and a parameter as defined by an example constructor of the feature class is called "position parameters". The other is a parameter assigned by an instance public domain or instance attribute defined in the feature class, called "specified parameters".

See the following implementation characteristics class: public class HelpAttribute: Attribute {public HelpAttribute (string url) {this.url = url;} public string Topic = null; private string url; private int number = -1; public string Url {get { Return URL;}} public int number {get {return number;}}}}}}} The following statement is used to use the following statement: HELP ("http://www.ccw. com.cn, Topic = "a demo class", number = 120). Where "http://www.ccw.com.cn" is a location parameter; Topic = "a demo class" is a specified parameter, corresponding to Topic public domain in the Helpattribute class; "Number = 120" is also a specified parameter, corresponding to Helpattribute Number public attributes in the class. The instance constructor of the position parameter and the corresponding feature class is closely related - what kind of parameter construction is provided, and the location parameters correspond to what kind of form. The location parameters are not omitted, but if the feature class provides a constructor without parameters, it is another matter. The specified parameter corresponds to the instance of the characteristic class, but it is not necessary when instantiated, and can be omitted. The location parameters and specified parameters also require their data types, which is mainly to ensure initialization of each parameter in parentheses "[]" instantiation statements. They must be one of the following types: eight integer types Sbyte, short, int, long, byte, ushort, uint, and ulong; character type char and Boolean Bool; two floating point type float and double; system.object (ie Object) and System.Type (TypeOf operator return value); enumeration type. Attributeusage Features AttributeUSAGE feature class is one of the three feature classes of C # reserved (other two are conditional features ConditionaAttribute and discard feature "OBSoletTribute). AttributeUSAGE feature class is used to describe how to implement the features of your own, such as what we implement is used for description methods, or class, interface? Whether it allows the feature class to make multiple descriptions of the same programming element (such as a writer of a class)? Is a class after being described by the feature class, can this description be embodied in its inheritance subclass? AttributeUSAGE features include the above behavior by three properties: Validon, AllowMultiPle, Inherited. It should be pointed out that the AttributeUSAGE class can only be used to directly or indirectly inherit from the System.attribute class, otherwise the compiler will report an error.

转载请注明原文地址:https://www.9cbs.com/read-121265.html

New Post(0)