C # attribute ----------- elementary

Source: Internet
Author: User

C # attribute ----------- elementary
Preface:

Attribute is a technique proposed by. net FrameWork. You can add annotations to your code to implement some special functions.

I. attribute function

Attribute is translated as a feature. Since it is a feature, the feature is not common.

1. Microsoft's statement

Attribute function provided by MSDN: The Attribute Class associates predefined system information or user-defined custom information with the target element.

The predefined system information is the system attribute provided by. net, and the user-defined information is the User-Defined attribute. The target element refers to the metadata element TypeDef (class, structure, enumeration, interface, delegate), MethodDef, ParamDef, FieldDef, PropertyDef. And so on.


2. Your own views

Whether it is a custom Attribute or a custom Attribute, it aims to enhance the modification of your target element. Like public, private modifiers are used to control access to metadata. Annotations are used to help others read and understand the code. When Attribute is applied, metadata needs to be modified. The question is, what kind of features can Attribute provide, so that we must use it to modify metadata?

1) set our Assembly information

When we create a project, the system will automatically generate an Assemblyinfo. cs. This file applies a large amount of Attribute to provide us with assembly information.

[Assembly: AssemblyTitle ("AttributeTest")] // name
[Assembly: AssemblyDescription ("")]
[Assembly: AssemblyConfiguration ("")]
[Assembly: AssemblyCompany ("Microsoft")] // assembly Company

2) ConitionAttribute, which is often used during testing, has many methods to be tested in a project. You classify it into several categories and then perform tests separately. I can't test this type of code, so I can comment out other codes. This is too low. In this case, the ConitionAttribute feature can be used.

Mark different features for various types of code. Then use # define AttributeName at the beginning. If you want to test which Attribute name is used.

Eg.

There are two methods in the project

[Conition ("A")]

Public void method1 ()

Public void method2 ()

[Conition ("B")]

Public void method3 ()

Public void method4 ()

Add # define A at the beginning

In this way, only method1 (), method2 () will be identified during running ()

3) the above two types are Custom Attributes provided by the system. When will the custom Attribute be used?

When there is a need, when other things cannot be implemented, you can consider custom Attribute.

For example, I am now working on the entity classes of database tables. In this way, when getting data, I am bound to the entity classes without assigning values one by one. The columns in the Database correspond to the attributes of the object class. When you select a table, bind the result to the object class. You need to traverse the object class attributes. Therefore, you must ensure that the object class contains attributes rather than fields or other attributes. So how do you do this? How do you modify this metadata so that one of them is an attribute? custom attribute

[AttributeUsage (AttributeTargets. Property)] // specifies that the target element can only be an attribute.
Class RetriveFromDb: Attribute
{

}


[RetriveFromDb]

Public string ID {get; set;} // is property, compiled through


[RetriveFromDb]

Public string GRADE = "98"; // field, compilation failed

This article only explains the Attribute function. Next I will explain the features of Attribute and how to use 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.