I'm making the assumption that IObject
/ IDataObject
are the same thing - otherwise it is hard to see how the call would ever work. So, the easiest thing to do is to make the caller supply the T
:
public static bool AddObject<T>(this T obj) where T : IObject, new()
{
return DataProvider.InsertObject<T>(obj, DataProvider.GetConnection());
}
However, this is not always workable (the caller might only know about IObject
), in which case you can also get the runtime to worry about it:
public static bool AddObject(this IObject obj)
{
return DataProvider.InsertObject((dynamic)obj, DataProvider.GetConnection());
}
The only other option is reflection via MakeGenericMethod
/ Invoke
- messy and slow.
Frankly, I would advocate a non-generic API here. Reflection and generics do not make good friends. However, ServiceStack may not give you this luxury, in which case, the dynamic
approach is probably your most convenient option here.