Let\'s say I have a type, MyType. I want to do the following:
Using reflection (and some LINQ) you can easily do this:
public static IEnumerable 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 are getting the interfaces on the type and filtering out only for those that are a generic type.
Then, you get the generic type definition for those interface types, and see if it is the same as IList<>
.
From there, it's a simple matter of getting the generic arguments for the original interface.
Remember, a type can have multiple IList
implementations, which is why the IEnumerable
is returned.