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