C#attribute-----------Primary

Source: Internet
Author: User

Objective:

attribute is the technology presented by the. NET FrameWork that adds annotations to your own code, enabling you to implement special features.

I. Attribute function

attribute is translated into characteristics, since it is a feature, the inevitable function is not general.

1. Microsoft's statement

MSDN gives the attribute feature: the attribute Class Associates predefined system information or user-defined custom information with the target element.

The information for predefined systems is the system attribute given by. NET, and user-defined information is user-defined attribute. The target element is the element typedef (class, structure, enumeration, interface, delegate) of the metadata, methoddef,paramdef,fielddef,propertydef. And so on.


2. Own views

Whether it's a custom attribute (System) or a custom attribute, its purpose is to enhance the grooming of your target elements. It's like public,private. This modifier is intended to control access to metadata. Comments are intended to make it easier for others to read and understand the code. So the application of attribute is when I need to decorate the metadata. So the question is, what kind of features does attribute provide that we have to use to decorate the metadata?

1) Set up our Assembly information

When we create a project, the system automatically generates a Assemblyinfo.cs, which applies a large number of attribute, providing us with assembly information

[Assembly:assemblytitle ("Attributetest")]//Name
[Assembly:assemblydescription ("")]
[Assembly:assemblyconfiguration ("")]
[Assembly:assemblycompany ("Microsoft")]//Assembly Company

2) often used in testing, Conitionattribute, a project needs to be tested in a number of ways, you classify him into several categories, and then test separately. You can't always test this kind of code and comment out other code. It's too low to do it. The Conitionattribute feature can then be used

Different types of code are labeled. Then use the # define AttributeName at the beginning. You want to test which class to use which kind of attribute name.

eg

There are two kinds of methods in engineering

[Conition ("A")]

public void Method1 ()

public void Method2 ()

[Conition ("B")]

public void Method3 ()

public void Method4 ()

Then add # define a at the beginning

This will only recognize Method1 (), METHOD2 ()

3) The above two are system-provided custom attribute, then when to use the custom?

You can consider customizing attribute when there's a need for something else that can't be realized.

For example, I now do the entity class of the database table, so that when the data is bound to the entity class, do not have to assign a value. The columns in the database correspond to the attributes of the entity class. When you bind the result to an entity class in the Select table, you need to traverse the entity class properties, so you must make sure that the entity class is a property rather than a field or something else. So how do you do this, how do you modify the metadata so that it must be a property: custom attribute

[AttributeUsage (Attributetargets.property)]//specifies that the target element can only be a property
Class Retrivefromdb:attribute
{

}


[Retrivefromdb]

public string ID {get; set;} Is the property, compiled by


[Retrivefromdb]

public string GRADE = "98";//field, compilation does not pass

This article only explains the attribute function. Follow-up explanation of attribute features and how to use

C#attribute-----------Beginner

Related Article

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.