C # You know I don't know attribute

Source: Internet
Author: User

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

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.