Desktop Developer, "Use Custom Attributes," by Steven Yi
Visual Studio Magazine, January 2003


The companion code for this article includes a console application in C#.

The source code contains all the class definitions for utilizing custom attributes and Reflection described in the article:

*) TestSort.cs is the main test program that instantiates a NameCatalog, assigns catalog items, and outputs the sorted items based on name.  The TestPriceCatalog() is also included to illustrate the same technique with a PriceCatalog.

*) AbstractCatalog is an abstract class that defines a utility method to generate pre-populated catalog item elements and creates a reference to DynamicSorter.

*) NameCatalog and PriceCatalog both extend AbstractCatalog, defining different CatalogSortAttribute settings in each class definition that will affect how catalog items are sorted.

*) CatalogItem represents an item in the electronic catalog.  There are properties defined for Name, Description, Item ID, and price.

*) CatalogSortAttribute is custom attributed created for the purpose of recording how items are sorted.  This attribute is used by the NameCatalog and PriceCatalog.

*) DynamicSorter reads the attribute information and uses Reflection and a quick-sort algorithm to sort the catalog item elements.

