1. The Attribute class Associates pre-defined system information or user-defined custom information with the target element.
2. The information provided by the feature is also known as metadata.metadata can be checked by the application at run time to control how data is handled by the program, or it can be checked by an external tool before it is run to govern how the application processes or maintains itself. for example, the. NET Framework pre-defined attribute types and use attribute types to control run-time behavior, some programming languages use attribute types to represent language features that are not directly supported by the. NET Framework general type System.
3. All feature types, directly or indirectly, areThe Attribute class derives. attributes can be applied to any target element, multiple attributes can be applied to the same target element, and attributes can be inherited by elements that derive from the target element. use the AttributeTargets class to specify the target element to which the attribute applies. The target element can be an assembly, class, constructor, delegate, enumeration, event, field, interface, method, portable Executable module, parameter, property, return value, structure, or other attribute.
Test code:
1 Public class Program2 {3 Public Static voidMain (string[] args)4 {5Test T =NewTest ();6Type type =T.gettype ();7 foreach(MethodInfo methodinchtype. GetMethods ())8 {9 foreach(Attribute attrinchAttribute.GetCustomAttributes (method))Ten { One if(attr. GetType () = =typeof(Someattribute)) A { -Console.WriteLine ("{0} has attribute is {1}", method. Name, ((Someattribute) attr). someone); - } the } - } - Console.read (); - } + } - Public classTest + { A [Some (SOMEONE.A)] at Public voidAmethod () - { } - [Some (someone.b)] - Public voidBmethod () - { } - [Some (someone.c)] in Public voidCmethod () - { } to } + [AttributeUsage (AttributeTargets.All)] - Public classSomeattribute:attribute the { * $ PublicSomeattribute (SomeOne o)Panax Notoginseng { - This. someone =o; the } + PublicSomeOne SomeOne {Get;Set; } A } the Public enumSomeOne + { - A, $ B, $ C -}
Operating effect:
C # You know I don't know attribute