Is there a way to detect the type specified in the general parameter in the class?
For example, I have three classes below:
public class Customer { } public class Repository<T> { } public class CustomerRepository : Repository<Customer> { } public class Program { public void Example() { var types = Assembly.GetAssembly(typeof(Repository<>)).GetTypes();
For each repository object returned, I would like to indicate what type is specified.
Is it possible?
EDIT
Thanks to @CuongLe, I got this that works, but it looks dirty .... (also help from resharper;))
var types = Assembly.GetAssembly(typeof(Repository<>)) .GetTypes() .Where(x => x.BaseType != null && x.BaseType.GetGenericArguments().FirstOrDefault() != null) .Select(x => x.BaseType != null ? x.BaseType.GetGenericArguments().FirstOrDefault() : null) .ToList();
Alex
source share