C # getting started with features

Source: Internet
Author: User

1. What is a feature?

CLR allows you to add a description declaration similar to a keyword, called attributes, which marks elements in a program, such as types, fields, and attributes. The metadata of the attributes and. NET Framework files is stored together and can be used to describe your code to the runtime, or affect the behavior of the program when the program is running.

Application: serialization, program security features, prevent the instant compiler from optimizing the program so that the code is easy to debug, and so on.

Attribute is a class

2. Attribute Class

A constructor: protected attribute ();

Three static methods: getcustomattrites (); getcustomattibutes (); isdefined ();

Two instance methods: isdefaultatti.pdf (); match ();

 

Attribute naming rules: "name" + "attribute ";

When your attribute is applied to an element of a program, the compiler first looks for your attribute definition. If it is not found, it will be searched by "name" + "attribute.

That is: When defining a feature, we add the suffix "attribute". When referencing it, we only need our feature name. We do not need to add the suffix "attribute.

3. Define and control the use of features

Set the element type to be controlled by the feature:

A feature is referenced before the class definition. Its name is attributeusage.

For example:

1.[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]2.public class HelpAttribute : Attribute3.{4.    public HelpAttribute(String Description_in)5.    {6.        this.description = Description_in;7.    }8.9.    protected String description;10.11.    public String Description12.    {13.        get14.        {15.            return this.description;16.        }17.    }18.}

Attributeusage class: three attributes: validon: Specifies the element type of the feature to be used. The value must be the enumerated value of attributetargets. (Introduced in msdn) The default value is all.

 

Allowmutible: whether to allow attributes to be used multiple times on the same element (false by default) inherited: whether the derived class can be inherited (false by default)
Positioning parameters and naming parameters: positioning parameters: the parameters for Constructor naming parameters are non-static public fields and attributes. Positioning parameters are required and naming parameters are optional. The usage of the two is different. You can directly specify the value for the positioning parameter. The format of the name parameter is: attribute name = "value"



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.