I want to implement a general method for retrieving header / detail data from a database:
public static T RetrieveHeaderDetail<T> where T : Header<???>, new()
Here is a description of the general view representing the title of the document:
public class Header<TDetail> where TDetail : class, new() { public List<TDetail> Details; }
And here are a few instances:
public class RequestForQuotation : Header<RequestForQuotationDetail> { ... } public class Order : Header<OrderDetail> { ... } public class Invoice : Header<InvoiceDetail> { ... }
It is easy to prove that since .NET does not allow multiple inheritance or “general specialization” (which would allow Header<U> to derive from some other Header<V> ), for any particular T there is at most one U , so T inherits ( directly or indirectly) from Header<U> . Moreover, it is trivial to find type U : iterate over the base types of T until you find an instance of Header<U> , and then just take a general argument! However, C # wants me to point out a change in my method definition to the following:
public static T RetrieveHeaderDetail<T,U> where T : Header<U>, new() where U : class, new() {
Is there any way around this problem? I know that it would be possible to use Reflection, but I believe that at runtime it is not recommended to do what could be done at compile time.
When I run into such problems, I really miss C ++.
source share