It is not possible to tell the compiler that T inherits from Record<T> . The reason is simple: because it is not, generally speaking. In fact, your Get method could theoretically be called with any argument, not necessarily inherited from Record<T> .
The fact that T really inherits from Record<T> only becomes known at run time when your method is actually called. Therefore, the necessary checks and designs should take place at the same time.
interface IHelper { object Fetch(object key); } class Helper<T> : IHelper where T : Record<T>, IRecordBase, new() { public object Fetch(object key) { return Record<T>.FetchByID(key); } } public virtual T Get<T>(object primaryKey) where T : IDalRecord, new() { var helperType = typeof( Helper<> ).MakeGenericType( typeof( T ) );
Keep in mind that it is completely (and even recommended) to cache those Helper<T> objects for performance.
You can also just use the old dumb reflection: find the FetchByID method by name and name it dynamically. But this can lead to a huge decrease in performance.
source share