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)