C # Get attributes

Source: Internet
Author: User

 

Pay attention to how to inherit from the system. Attribute Class and How to Use constructors and attributes to define attribute Parameters
You can use this attribute method in the VB class as follows:
<Author ("Julian")>
Public class Fred
...
End Class
In C ++, you can use attribute to create a Managed class or structure.
To represent a custom attribute. Note that this class does not need to be inherited from system. attribute.

[Attribute (target)]
Public _ GC class author {
...
};
If attribute is to be used in other assemblies, the class must be a public target.
The parameter is system. A parameter of the attributetargets enumeration is shown in Figure 2.5. In the attribute class, the constructor is used to specify the location parameter, while the data member and attribute are used to implement the named parameter. below is the c ++ of the author attribute. Code
[Attribute (class)] public _ GC class author {string * authorname; // positional parameter string * lastmoddate; // named parameter public: _ property string * get_name () {return authorname;} _ property string * get_moddate () {return lastmoddate;} _ property string * set_moddate (string * Date) {lastmoddate = date ;} author (string * Name) {authorname = Name ;}}; you can add this attribute to a class as follows [author ("Julian ", date = "21/12/00")] public _ GC class Foo {}; we can see that data is a named parameter. It is specified by the keyword and located at the end of the parameter list. The method for creating attribute in C # is similar, but there are two different [attributeusage (attributetargets. class)] public class Author: system. attribute {private string authorname; // positional parameter private string lastmoddate; // Chapter 2nd of named parameter. NET Programming Model 71 public string name {get {return authorname;} Public String moddate {get {return lastmoddate;} set {lastmoddate = value ;}} author (string name) {authorname = Name ;}}; The first difference is that the attribute class starts from system. attribute inherited from C ++ does not require any inheritance. The second difference is that attributeusage attribute is used to control the scope of use of this attribute. In addition, the code structure is similar to where constructor is implemented. parameters use properties to implement named Parameters

2.2.19 query attribute
Although most attributes are created by the compiler and used by the CLR, sometimesProgramYou also need to know the scope of certain items that process specific attributes and be able to check these attributes. In order to find the relevant attributes, you need to understand system. the type class type is a type that represents the property value type of the class interface and enumeration type definitions. It can find a lot of detailed information about the type and its attributes, but you only need to know how to use the class to obtain these attribute information. If you understand C ++, you can consider using rtti. The following describes how to retrieve the attribute information in VB and analyze the code. The author in the previous section is still used. attribute instance <author ("Julian ", date = "21/12/00")> public class FOO... end Class obtains a type object to indicate the foo class, and then queries the class to know whether the foo class has an author attribute.

In C #, this operation is almost identical.

 

  Using  System;
...
Type TF = Typeof (FOO );
Object [] ATTS = TF. getcustomattributes ( True );
Foreach ( Object O In ATTS ){
If (O. GetType (). Equals ( Typeof (Author )))
Console. writeline ( " Foo has an author attribute " );
}

 

 

 

The first thing is to use the typeof operator to obtain the type object of the class to be queried.
Then, you can use the getcustomattributes () method to obtain a reference list of the Custom Attributes supported by this object.
Because an array referenced by a common object is returned, you need to check the object type to check whether there are author items in them.
Note that using the GetType () method will return a type object from the object reference.
This corresponds to the typeof operator. The latter returns an author attribute from the class name.
Once an author attribute is found for this class, this parameter can be retrieved.

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.