"Inside C #" note (vii) Attribute

Source: Internet
Author: User

Attribute characteristics can be said to have a new meaning, because the general language in the design, it has the ability to be fixed. With the attribute feature, we can append information to the existing types of C #, either at programming time (Design-time), as well as attaching runtime (Run-time) information (such as database field mappings), You can also use attribute to control the behavior of your code, such as permission checks. The attribute can offer unlimited possibilities.

A attribute statement.

Before you use the attribute tag, you first define it

The above code is the way attribute is declared, [RegistryKey] Behind the label is a class Registrykeyattribute that inherits the System.Attribute, the constructor of the class has two parameters, then the use of the corresponding to pass two parameters

The contract Attribute declares when the class name uses ***attribute, ending with Attribute, which saves the end of Attribute when used.

The use of two attribute

In order to find the attribute attached to a type, it needs to be reflected in a way.

A) attribute on the class

Define a attribute as follows

And then in the Myremoteableclass tag

So, Remoteobjectattribute can be used in this way.

After using typeof to obtain the type, use the. The GetCustomAttributes () method can get all the additional attribute

b) attribute on the method

For example, there is a transactionableattribute tag on the method.

How to use it differs from a class

After you get to the type, you also use the type. GetMethods () Gets the MethodInfo of the method inside the class, and then uses GetCustomAttributes.

c) attribute on the field

Similarly, you need to use type first. GetFields () Gets the FieldInfo of all fields.

Parameters of three attribute

A) in the previous Registrykeyattribute, two parameters were used.

However, if the parameters are not constantly changing, they can be transformed into named parameters (named parameter), such as the first parameter, can be changed from the constructor to the property, if necessary, you can set the value separately.

It should be noted that named parameters must be placed after the fixed parameters, fixed parameters can not be similar to the wording of named parameters, because the compiler is to handle the way to find the named parameters, and then the remaining parameters as a fixed parameter, if the fixed parameters are also named parameters to write, the compiler does not know how to assign a constructor function. Named arguments can be accessed by a type that includes all exposed fields or properties, and also includes setter methods, but these cannot be static or const-decorated.

b) The types that the attribute parameter can use include value types, System.Type,. Object, enumerations, one-dimensional arrays (the types of array elements can only belong to the previously enumerated types), but cannot be classes (class). Because attribute is used when encoding, it is a class that is not instantiated.

"Inside C #" note (vii) Attribute

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.