An attribute enables you to add some information, or metadata, to classes,
properties, methods or events at compile time which can be used by the
reflection system at runtime.
A good example of this is the BrowsableAttribute which can be added to a
property and which controls the visibility of properties for the property
grid. When an object is passed to the property grid's SelectedObject
property, the grid uses reflection to get a property descriptor for each of
the public properties the object provides, it filters these property
descriptors, only keeping the ones with the [Browsable(true)] attribute and
uses their names, property types and values to populate the grid.
The property grid also uses the EditorAttribute to determine how a value
might be edited, for example, the Color structure has had an editor
attribute applied that informs the property grid that Color structures will
be edited with the colour UITypeEditor.
You can create your own attributes too that can be used to provide custom
information for classes, methods, properties and so-on. The attributes for a
particular thing may be discovered by using reflection. the TypeDescriptor
class enables you to get a collection of property descriptors for a class.
Each property descriptor has an Attributes collection that contains all of
the attributes applied to each property.
Because attributes add data to something at compile-time, you cannot modify
them at runtime. This means that for example, you cannot do something like
(pseudocode)
TypeDescriptor.GetProperty("fred").Attributes["Broswsable"]=true;
Hope this unmuddies the waters a bit...
--
Bob Powell [MVP]
C#, System.Drawing
The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
Read my Blog at
http://bobpowelldotnet.blogspot.com