I think JaredPar had the right idea the first time around.
interface IMyTable<T>
{
Table<T> TheTable {get;}
}
public class MyClass<TContext,T> where
TContext : DataContext,IMyTable<T>
{
//silly implementation provided to support the later example:
public TContext Source {get;set;}
public List<T> GetThem()
{
IMyTable<T> x = Source as IMyTable<T>;
return x.TheTable.ToList();
}
}
I want to extend his thought by adding an explicit interface implementation. This addresses Jason's remark about accessing the table property through IMyTable. The type must be involved somehow, and if you have an IMyTable<T>
, the type is involved.
public partial class MyDataContext:IMyTable<Customer>, IMyTable<Order>
{
Table<Customer> IMyTable<Customer>.TheTable
{ get{ return this.GetTable<Customer>(); } }
Table<Order> IMyTable<Order>.TheTable
{ get{ return this.GetTable<Order>(); } }
}
Now it is possible to do this:
var z = new MyClass<MyDataContext, Customer>();
z.Source = new MyDataContext();
List<Customer> result = z.GetThem();