I m having to modify a lot of code to set a SqlCe database connection for a bunch of table adapters. I would like to avoid the duplication involved and minimise the chance I ve missed updating a table adapter by creating a generic method to wrap the creation of each table adapter. Unfortunately the table adapters aren t derived from a single base class and each has it s own Connection property. By assigning the new table adapter to dynamic I can get it to compile. Is this a good approach and are there any traps I should be aware of when using dynamic?
protected void TableAdapter<T>(Action<T> action) where T : IDisposable, new()
{
using (var connection = new SqlCeConnection(ConnectionString))
using (dynamic tableAdapter = new T())
{
tableAdapter.Connection = connection;
action(tableAdapter);
}
}