Creating and Examining Custom Code Attributes

Creating custom attributes is actually a pretty simple process. A custom code attribute is really just a class that inherits from System.Attribute. After you create a class based on the Attribute class, you can then decorate additional classes with that attribute to serve as metadata that can be read at runtime.

There are many reasons why you would want to associate custom attributes with your code. Some of the attributes that are already part of the framework allow you to control the transactional behavior of classes, the serialization behavior, the COM visibility of other classes, and much more.

Listing 11.4 shows an example of a class that inherits from Attribute. You use private members, constructors, and properties for providing access to the metadata information that will be supplied to the class at runtime through attribute parameters.

Listing 11.4. Custom Code Attributes: The DataColumnAttribute Class

using System; using System.Data; using System.Collections.Generic; using System.Text; namespace CodeAttributeDemo { [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] class DataColumnAttribute : Attribute { private string columnName = "Column"; private System.Data.DbType dataType = DbType.String; public DataColumnAttribute() { } public DataColumnAttribute(string column, DbType dbtype) {     columnName = column;     dataType = dbtype; } public string ColumnName {     get { return columnName; }     set     {         columnName = value;     } } public DbType DataType {     get { return dataType; }     set { dataType = value; } } } } 

It looks like a fairly simple class. In fact, it is quite simple. All it's maintaining is a couple of bits of information about how to relate a property on a class to a column in a database, a common data access task. Listing 11.5 shows a class called Shape that has attributes that show what it looks like when you associate custom code attributes with a class.

Listing 11.5. Custom Code Attributes: The Shape Class (Attribute Decoration Sample)

using System; using System.Collections.Generic; using System.Text; namespace CodeAttributeDemo { [DataTable("tblShapes")] class Shape {     private string name = "Square";     [DataColumn("Name", System.Data.DbType.String)]     public string Name     {         get { return name; }         set { name = value; }     } } } 

Finally, Listing 11.6 shows how you can use reflection to obtain references to the custom code attributes on a class instance at runtime.

Listing 11.6. Using Reflection to Query Custom Attributes at Runtime

[View full width]

using System; using System.Reflection; using System.Collections.Generic; using System.Text; namespace CodeAttributeDemo { class Program { static void Main(string[] args) {     Type t = typeof(Shape);     DataTableAttribute[] attribs = (DataTableAttribute[]) t.GetCustomAttributes(typeof(DataTableAttribute), true);     DataTableAttribute attrib = attribs[0];     Console.WriteLine("The Shape class is persisted via the {0} table in a database.",  attrib.TableName);     DataColumnAttribute[] cols = (DataColumnAttribute[])        t.GetProperty("Name").GetCustomAttributes( typeof(DataColumnAttribute), true);     Console.WriteLine("The 'Name' property, when persisted to a database, is called {0},  and is of type {1}",         cols[0].ColumnName, cols[0].DataType.ToString());     Console.ReadLine(); } } } 

Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298 © 2008-2017.
If you may any questions please contact us: