Metadata Attributes


The .NET Framework attribute construct allows you to supply metadata for your component and its members . The information contained in the metadata attribute is queried using reflection by the runtime or by design-time tools.

Let's look at the syntax for the attribute construct. In the following example, the decoration (shown in square brackets in C#, and in angle brackets in Visual Basic .NET) is an instance of the attribute class System.ComponentModel.BrowsableAttribute . BrowsableAttribute is a design-time metadata attribute that tells the designer whether or not to display a property in the property browser.

Here's the C# example:

  [Browsable(false)]  publicintHits{...} 

Here's a Visual Basic .NET example:

  <Browsable(false)  >PublicHitsAsInteger 

By convention, attribute class names end with the Attribute suffix, such as BrowsableAttribute , DefaultValueAttribute , DesignerAttribute , and ParseChildren ­Attribute . In C# and Visual Basic .NET, the Attribute suffix is implied and can be omitted from the name of the attribute class when applying an attribute. For example, Browsable (false) is equivalent to BrowsableAttribute (false).

When programming server controls, you will work with the set of attribute classes that are listed in Appendix A, "Metadata Attributes." Server controls are marked with both design-time and run-time attributes. Run-time attributes (such as ParseChildrenAttribute ) are essential for the functioning of an ASP.NET control during a request/response cycle, while design-time attributes (such as BrowsableAttribute and DesignerAttribute ) enhance the design-time appearance and behavior of a control in a visual design tool. We will explain specific attributes in greater detail in context wherever they are applied.

Applying Attributes

Attributes in server controls are applied at the class level as well as to properties, methods , and events. Attributes can also be applied to an assembly. In Chapter 5, we'll show you how to apply the System.Web.UI.TagPrefixAttribute attribute to an assembly that contains server controls.

Class-Level Attributes

The following example applies a class-level design-time attribute named DesignerAttribute to the MyControl class. The DesignerAttribute , which we will describe in Chapter 15, enables you to associate classes called designers with your components . Designer classes govern the design-time appearance of your control.

Here's the C# version:

 //Attributethatassociatesthedesignerclass //MSPress.ServerControls.Design.MyControlDesigner //withMyControl. [Designer(typeof(MSPress.ServerControls.Design.MyControlDesigner))] publicclassMyControl:WebControl{//...} 

And here's the Visual Basic .NET version:

 'Attributethatassociatesthedesignerclass 'MSPress.ServerControls.Design.MyControlDesigner 'withMyControl. <Designer(GetType(MSPress.ServerControls.Design.MyControlDesigner))>_ PublicClassMyControl InheritsWebControl '... EndClass 
Property-Level and Event-Level Attributes

Here's a C# example that shows how to apply an attribute to a property:

 //ToapplyCategoryAttributetotheMyBorderColor //property,placeitbeforethedeclaration //oftheMyBorderColorproperty.  [Category("Appearance")]  publicColorMyBorderColor{...} 

And here's a Visual Basic .NET example:

 'ToapplyCategoryAttributetotheMyBorderColor 'property,placeitbeforethepropertydeclaration. <  Category("Appearance  ")>PublicMyBorderColorAsColor 

Attributes can be applied to event members in much the same way that they are to property members.

Here's a C# example that shows how to apply an attribute to an event:

 //ToapplyDescriptionAttributetotheClickevent, //placeitbeforetheeventdeclaration.  [Description("TheClickeventofthebutton")]  publiceventEventHandlerClick; 

And here's a Visual Basic .NET example:

 'ToapplyDescriptionAttributetotheClickevent, 'placeitbeforetheeventdeclaration. <  Description("TheClickeventofthebutton  ")>PublicEventClick 

We will discuss design-time attributes in general in Chapter 15 and design-time attributes applied specifically to properties in Chapter 7 and Chapter 10.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net