MVC summary-Features (Attributes)

Source: Internet
Author: User

1. Definition

MSDN Definition: The common language runtime allows you to add a descriptive declaration like a keyword, called attributes, that labels elements in your program, such as types, fields, methods, and properties. Attributes and Microsoft. NET Framework file metadata are saved together to describe your code to the runtime, or to affect the behavior of your application as it runs. Examples of specific use are:

[Obsolete]//This property is the deprecated public string Demo () {    return "";}

The method is preceded by the [Obsolete] attribute, which prompts the "method is obsolete" when calling the method.

2. Application Scenarios

An attribute can be paired with a reflection method to return information that refers to the attribute method or class that describes the presentation of the Reference object. Specific as follows:

    Custom author attribute public class Author:System.Attribute {public string Name {get; set;}            Name public double version {get; set;}//Version public Author (string _name, double _version) { This.            Name = _name; This.        Version = _version;        } public string GetInfo () {return "name:" + name + ", Version:" + versions;    }}//reference attribute class [Author ("HJX", 1.1)] class FirstClass {//...} Use the reflection method to invoke the attribute output class Information public string Index () {//Call the Print author method string excultresult = Printauthorinfo (ty            Peof (FirstClass));    Returnexcultresult;            }//The concrete reflection method implements the private static string Printauthorinfo (System.Type t) {string Result = "";              system.attribute[] Attrs = System.Attribute.GetCustomAttributes (t);                    foreach (System.Attribute attr in attrs) {if (attr is Author) {Author a = (Author) attr;                Result = A.getinfo ();        }} return Result;     }

Summary: Features, in addition to describing object information, can also be implemented in conjunction with filter (filter) for AOP programming, as described in AOP programming-filter (filters)

MVC summary-Features (Attributes)

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.