C # sharp experience the 15th lecture

xiaoxiao2021-03-06  73

Characteristics of the 15th lecture

Characteristics (attribute) is an exciting innovation that C # is introduced for component programming, which allows us to provide additional descriptive information for various elements of the program, such as classes, structures, interfaces, methods, and the like. These descriptive information can be taken by us when the program code is run. Let's look at a sample program: using System; public class AuthorAttribute: Attribute // Characteristics of the class {public AuthorAttribute (string name) {this.name = name;} public string Name {get {return name;}} private string name } ["Anders Hejlsberg"] // Features Instantiate Class Myclass {Int Count; Public MyClass (INT Count) {this.count = count;} public int count {incount;}}} Class Test {Static void main () {type t1 = typeof (myclass); type t2 = typeof (authorattribute); Object [] arr = t1.getcustomattributes (T2, true); // Get feature instance (array) Console.Write (" ");" Object O IN arr) {authorattribute aa = (authorattribute) O; console.write ("{0}", aa.name);} console.writeline ();}} First, declare and implement an AuthorAttribute feature class, and the feature class must be directly or indirectly self-abstract class System.attribute, which is often the first step in our feature. It will be the data structure we provide descriptive information. It is worth pointing out that the suffix "attrubute" in the AuthorAttribut feature class is a naming method of the C # recommended feature class, and it is not necessary. Next, when we realize your own class Myclass, we use the previous AuthorAttribute feature class to provide information for MyClass. Feature instantiation statement [Author ("Anders Hejlsberg")] Helps us to do this. Here is the middle bracket "[]" tells C # we are instantiated in progress. Author is an authorattribute class to remove the suffix "attrubute" - C # compiler can be automatically identified, of course, we will replace Authorttribute after the effect is the same. Among them, "Anders Hejlsberg" will call the AuthorAttribute (String Name) instance constructor of the AuthorAttribute feature class. After this feature is instantiated, we give our MyClass class with a "authorattribute" feature description. In the Test test class, we can obtain our previously set description information by mapping (which will be described below). 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 defined by the example constructor of the feature class above is called "position parameters". The other is the parameter of the assigned value of the example public domain or instance attribute given in the feature class, referred to as "specified parameters", see the implementation of the following characteristics: PUBLIC CLASS HELPATTRIBUTE: Attribute {public helpattribute (String URL) { THIS.URL = URL;} public string topic = null; private string URL; private intend = -1; public string url {get {return url;}} public int number {get {return number;} set {number = value }}} For such an instantiation of such a feature class, we often use the following statement: [HELP ("http://www.ccw.com.cn", Topic = "A Demo Class", Number = 120)] . Where http://www.ccw.com.cn is the 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 Number Public Properties in the Helpattribute class. The location parameter and the corresponding feature class instance constructor are closely related - what kind of parameter construction is provided, and the location parameters correspond to what kind of form. The location parameter is not omitted, and of course, if the feature class provides a parameter-free constructor, it should be another matter. The specified parameter corresponds to an instance or instance of the feature class, which is not necessary when instantiated, and can be omitted. The location parameters and specified parameters also have the types of their types, mainly to ensure that the initialization of each parameter is completed in brackets "[]" 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 feature class AttributeUSAGE features One of the three feature classes of C # reserved, and the other two are conditional feature classes ConditionaAttribute and discard feature class ObsoleTeattribute. AttributeUSAGE features use to describe our own feature class "How to describe other classes", such as the characteristics we implemented for describing methods, or class, interface? It is also allowed to allow our characteristics to make multiple descriptions of the same programming element (such as a writer of a class)? Is this description that can be reflected in his inheritance subsystem after being described by our characterization class? AttributeUSAGE features include the above behavior by three properties: Validon, AllowMultiPle, Inherited.

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

New Post(0)