. Net short description features (Code attributes)

Source: Internet
Author: User

Since I haven't written about basic knowledge for a long time, let's summarize it today. the concept of Code attributes in the. NET platform is only a basic introduction. The purpose of this article is to guide beginners to use features, rather than to discuss advanced underlying technologies. [Wang qingpei is copyrighted. repost this article with a signature.]

1. What is feature attribute?

Features are common. net programmers are no stranger. Since we started to access the C # language, we have been more or less familiar with the use of features; but for beginners, we really don't understand the meaning of features, whether a feature belongs to a class or a class instance. These concepts are unclear about the logic of understanding the Code. This article will show you the role of the feature, in fact, the features are really convenient. At least I think that since I understood the features, my code is more user-friendly and the program structure is clear and easy to understand, so I often like to use some simple and clear style to express the characteristics of objects when writing code;

In fact, the feature attribute is a piece of code attribute attached to the defined object. The feature belongs to the object rather than the instance. Pay attention to this concept. Why do we need the feature, I don't think any developer needs to ask why, because it is good for us to clap our hands, and why not use good things; the reason you don't feel it is because you haven't discovered the advantages of a friend. Let's use the code example to facilitate two-way communication;

2. instance code learning

using System;using System.Collections.Generic;using System.Text;namespace AttributeTest{    [TableName(Name = "Tb_UserName")]    public class M_UserName    {    }}

In this Code, I Have A m_username class. I added a tablename feature to the class to identify the name of the database table corresponding to this class, such applications will usually appear in the Orm. tablename is a feature defined by us. Such a feature does not affect the compilation behavior of the system, and the system feature changes the compilation behavior of the Code, this compilation control is necessary, for example, we are doing this. net security programming, the securitypermissionattribute feature is often used to control the secure execution logic of the Code. The most important role of the operating system API is permission control, so that everyone can enter the kernel together; if features are not used for marking, all methods will be compiled in a unified manner and cannot be controlled. [Wang qingpei is copyrighted. Please give a signature for reprinting.]

We return to the tablename feature. The feature is a special type that inherits from the attribute object;

Using system; using system. collections. generic; using system. text; namespace attributetest {// <summary> // name of the database table /// </Summary> [attributeusage (attributetargets. class, allowmultiple = false, inherited = false)] public class tablename: attribute {// <summary> /// name /// </Summary> Public string name {Get; set ;}}}

The features we define also need to be marked with the attributeusage feature of the system, so that the system can treat the features we define as special types. Here, the feature is also a class type, it is only a special type, which needs to be described using system features so that the system will differentiate our features for metadata during compilation, we can get this special metadata through reflection;

Maybe you will ask, How can I mark the features of a class like this? Of course, this is useful. We will use reflection to dynamically read the features of the object; the feature belongs to the class type, that is, the type. We obtain the feature through the type instead of the instance;

Using system; using system. collections. generic; using system. text; using system. reflection; namespace attributetest {class program {static void main (string [] ARGs) {type = typeof (m_username ); // use the typeof method to obtain the type object [] OBJ = type. getcustomattributes (typeof (tablename), false); // obtain the specified tablename name = OBJ [0] As tablename through the type object; console. writeline (name. name); console. readline ();}}}

In this way, we get the value marked by the feature. In fact, you can regard the feature as a static class. When we get the feature, the system will load the feature class into the memory. I don't know what the layout of the feature class data is like in the memory, but when we first learned how to use it, it can be understood first;

Summary: The feature is a code attribute and is a special class. It exists in metadata as a special identity during compilation. When we get it, the system is loaded into the memory. The feature belongs to the class and does not belong to the class instance. [Wang qingpei is copyrighted. For details, please sign it.]

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.