010. Using the properties provided by the. NET Framework

Source: Internet
Author: User

C # allows you to declare attributes (classes) on classes and class members, and to interpret the members of classes and classes at run time. This feature is also called a property, using attribute. The following shows how to use the properties provided by the. NET Framework.

1 usingSystem;2 usingSystem.Reflection;3  Public Partial class_default:system.web.ui.page4 {5     //Custom Attribute Classes6[AttributeUsage (attributetargets.class, AllowMultiple =true)]7      Public classBookattribute:attribute//Attribute base class for custom properties8     {9         Private stringBookName;Ten          PublicBookattribute (stringbookname) One         { A              This. BookName =BookName; -         } -          Public string Book the         { -             Get -             { -                 returnBookName; +             } -         } +     } A  at     //using attribute classes more than once -[Book ("ASP. NET entry basics;")] -[Book ("C # practical Case")] -      Public classBookInfo -     { -          Public stringBinfo ="Book Information ***********"; in          PublicBookInfo () -         { to  +         } -     } the  *     //example of a Page_Load invocation of a page custom property $     protected voidPage_Load (Objectsender, EventArgs e)Panax Notoginseng     { -         //instantiate the BookInfo class theBookInfo BF =NewBookInfo (); +         //Object Array A         Object[] bookattr; the         //gets the object type, gets information about member attributes, and provides access to member metadata +MemberInfo TypeInfo =typeof(BookInfo); -         //extract type is a member of an attribute class $Bookattr = Typeinfo.getcustomattributes (typeof(Bookattribute),false); $         if(Bookattr.getlength (0) !=0) -         { -Bookattribute ba = (bookattribute) (bookattr[0]);//gets the first member of an attribute class theBookattribute Ba2 = (bookattribute) (bookattr[1]);//gets the second member of an attribute class - Response.Write (bf.binfo);WuyiResponse.Write ("<br/>"); theResponse.Write ("Book name:"+ BA. Book +Ba2. book); -         }    Wu     } -}

010. Using the properties provided by the. NET Framework

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.