I want to know if a type is IQueryable.
var t = typeof(IQueryable<int>);
bool isQueryable = (t is IQueryable); // false
bool isAssignableFrom = t.IsAssignableFrom(typeof(IQueryable)); // false
bool nameStartsWithIQueryable = t.Name.StartsWith("IQueryable"); // true
The third way – looking at the beginning of the type name – works but feels like a hack.
Is there another way to accomplish this?
Use
GetGenericTypeDefinition:If you need to handle deeper ancestries (where you are checking aganist the base type), you can write a helper method:
Calling it like so:
(Note that this won’t help with base types that implement other interfaces, since checking against that would require a lot more code using
Type.GetInterfacesrecursively.)