Example Of an Attribute

  • Thread starter Thread starter Justine
  • Start date Start date
J

Justine

Hi All,

I would like to know where & when exactly we would have a
need for attributes. A realistic example.


Thanz in Advance,
Justine
 
One example is their use by the XmlSerializer component. The XmlSerializer
reads the public data members of an arbitrary class instances, and
serializes them as XML. By placing specific attributes on the data members,
you can control how the XmlSerializer serializes each individual member.
For example, the XmlSerializer will pick a default element name based on the
name of the data member - but you can put the XmlElementAttribute attribute
on a data member, and set the ElementName property of that attribute, and
change the name of the XML element which gets written for that data member.

--Don
 
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
 
We've used custom attributes on object properties that we want to
persist when we write our objects out to disk. Using Reflection, we
just loop over the properties for the object, check to see if this
attribute is set and, if it is, write that property name and it's
value out to a file.


Additionally I've used a custom attribute to provide a "pretty name"
to properties that are displayed in a PropertyGrid. Normally, it just
displays the property name and requires all of the same restrictions
(primarily, no spaces). Using my attribute, I could specify an
arbitrary string to display as the property name.

Those are just 2 examples of how we've used them in our code.
 
Back
Top