Casting from a Collection to a Data Table using Generics and Attributes
by Joe Finsterwald
This code sample applies to ASP.NET in the .NET 2.0 framework. Examples are written in C#. 
Contents
- The Problem
- The Solution
- Step 1: Build your own Custom Attribute Class
- Step 2: Build a Container Class
- Step 3: Create an Interface--IDataTableConverter
- Step 4: Build a DataTableConverter Class
- Step 5: Build your own Generic List
- Step 6: Build your test harness
- Conclusion
The Problem:
You have a collection of classes that you want to convert into a DataTable.
The solution:
The following step by step example will demonstrate how you can use Aspect Oriented Programming (AOP) to solve this dilemma. This particular example will leverage the power of attributes, generics, and reflection to explicitly convert a collection of container classes into a DataTable.
I should state for the record that this article was inspired in part by a cogent book on the topic: Applied .NET Attributes by Jason Bock and Tom Barnaby.
Step 1: Build your own Custom Attribute Class
Custom Attributes always inherit from System.Attribute, in fact any class that inherits from System.Attribute whether directly or indirectly is an attribute class. Attribute classes also follow the convention of having the word “Attribute” attached as a suffix to the class name.
Attributes allow you to add metadata to an object that you can read at run-time via reflection. As a result, they provide an elegant
(and granular) solution to the object oriented problem of cross-cutting concerns.
Our first step will be to build a custom attribute class that will allow us to acquire meta data about the properties of our container class (which we haven’t built yet) at run-time. The beauty of this solution
is that we can use our custom attribute class (in this case ConversionAttribute) to decorate any class that we decide to add to our project at a later date.
Step 2: Build a Container Class
Now we create a container class! Notice how I've decorated the properties of this class with the attributes we’ve created in Step 1. In Step 4 we'll use this information to build a DataTable via reflection.
Step 3: Create an Interface--IDataTableConverter
For purpose of this example we’ll be converting a generic list (System.Collections.Generic.List) to a DataTable. The implementation for a dictionary might be different so we’ll want to leverage the power of an interface to abstract away from any specific implementation.
Step 4: Build a DataTableConverter Class
This is the class that will be doing all the work. Essentially, this class uses the System.Reflection namespace to query
attributes at run-time, build a DataTable schema, and fill it.
Step 5: Build your own Generic List
Here were we add the explicit conversion operator that will allow our list to be converted to a DataTable. If you want to
learn more about type conversions in C# I recommend you read the following (excellent) article by Rajesh V.S.: Type Conversions.
Step 6: Build your test harness
In this step we're going to fill our collection with Employees, explicitly convert to a DataTable, and then bind to DataGrid.
Conclusion:
Now you have the ability to explicitly convert any decorated container class from a List to a DataTable! Hopefully I’ve also piqued your interest in Aspect Orient Programming! If you have an comments or suggestions for improvement please let me know by leaving a comment in the comment section.