Using reflection (and some LINQs), you can easily do this:
public static IEnumerable<Type> GetIListTypeParameters(Type type) { // Query. return from interfaceType in type.GetInterfaces() where interfaceType.IsGenericType let baseInterface = interfaceType.GetGenericTypeDefinition() where baseInterface == typeof(IList<>) select interfaceType.GetGenericArguments().First(); }
First you get the interfaces by type and filter out only those that are generic types.
You will then get a generic type definition for these interface types and see if it matches IList<>
.
From here it is easy to get general arguments for the source interface.
Remember that a type can have multiple implementations of IList<T>
, so IEnumerable<Type>
returned.
casperOne Jul 13 '09 at 20:40 2009-07-13 20:40
source share