Share via


DynamicDataTable, Part 0

To commemorate the first beta release of the first CLR to contain the DLR, I’m going to try to implement something more interesting than useful – a dynamic version of the DataTable class. My goal is to show how the dynamic support in C#4 and VB10 can be used to create a better development experience, and to show how you can work effectively with IDynamicMetaObjectProvider.

The BCL’s DataTable is already fairly dynamic; its columns can vary at runtime in type and name. You can even add and remove columns on-the-fly. As such, it’s a pretty good candidate for “dynamicizing”. Of course, we can’t actually modify the existing DataTable implementation, so I’ll start out by creating a wrapper for it using DynamicObject. Where I hope to end up is with a self-contained implementation that implements its own DynamicMetaObject rather than leaning on DynamicObject and DataTable.

Here’s a sneak preview of Part 1:

     public static class Program {
        private static DataTable CreateTable() {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Value", typeof(int));

            table.Rows.Add("Bob", 23);
            table.Rows.Add("Karen", 17);
            table.Rows.Add("Allen", 10);
            table.Rows.Add("Grace", 31);
            return table;
        }
        public static void Main(string[] args) {
            DataTable table = CreateTable();
            dynamic t = new DynamicDataTable(table);
            t.NewName = t.Name;
            t.Name = "unknown";
            foreach (var r in t.NewName) {
                System.Console.WriteLine(r);
            }
        }
    }

See you again soon!

Comments

  • Anonymous
    May 23, 2009
    Let’s get started by doing “the simplest thing that could possibly work”. public class DynamicDataTable