[Asp.net Basics] attribute feature memo

Source: Internet
Author: User
What is attribute?

Attribute is a modifier that can be freely defined by the user and can be used to modify various objects to be modified.
In short, attribute is an "attachment"-just like an oyster adsorption on the bottom of a ship or on a reef.
These attachments are used to append additional information to their attachments (the information is stored in the body of the attachments) -- for example, "this class is written by me" or "this function has encountered problems before.

Role of attribute

The role of feature attribute isAdd metadata.
Metadata can be supported by tools. For example, the compiler uses metadata for compilation and the debugger uses metadata for debugging.Program.

Personal Understanding

Annotations are for people. attribute is for programs. Add annotations to an object and you can get this annotation through reflection during running.
To perform some operations.

Example

 
[Name ("Chinese name","Nicholas Tse"), Name ("English name","Nic")]
 
Public ClassStudent {}

Public ClassSchool {
 
[Name ("Chinese name","Nicholas Tse"), Name ("English name","Nic")]
 
Public StringSchoolname {get {Return "Niuqiang University";}}
 
}

 
[Attributeusage (attributetargets. All, allowmultiple =True)]
 
Public ClassName: system. Attribute
 
{
Public StringKey {Get; set ;}
 
Public StringValue {Get; set ;}
 
PublicName (StringKey,String Value){
 
Key = key;
 
Value =Value;
 
}
 
}

 
// Obtain the features of the class
 
VaR attrs = attribute. getcustomattributes (Typeof(Student ));
 
Response. Write ("<Br>");
Response. Write ("Class features");
 
Foreach(VAR itemInAttrs)
 
{
 
VaR t = (name) item;
 
Response. Write ("<Br>");
 
Response. Write (T). Key +":"+ T. Value +","+ T. typeid. tostring ());
 
}

 
// Obtain the attributes. This is also true for methods, fields, and other features.
 
VaR properties =Typeof(School). getproperties ();
Response. Write ("<Br>"); Response. Write ("<Br>");
 
Response. Write ("Attributes have");
 
Foreach(VAR itemInProperties)
 
{
 
Foreach(VAR ATTRInItem. getcustomattributes (False))
 
{
 
VaR t = (name) ATTR;
 
Response. Write ("<Br>");
 
Response. Write (T). Key +":"+ T. Value +","+ T. typeid. tostring ());
}
 
}

In this way, we get an instance of the name class at runtime. The name attribute is output.

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.