Use GetGenericTypeDefinition:
bool isQueryable = t.GetGenericTypeDefinition() == typeof(IQueryable<>);
If you need to handle deeper ancestors (where you check the type of base type aganist), you can write a helper method:
public static bool IsType(Type type, Type ancestor)
{
while (type != null)
{
if (type.IsGenericType)
type = type.GetGenericTypeDefinition();
if (type == ancestor)
return true;
type = type.BaseType;
}
return false;
}
The call is this:
bool isQueryable = IsType(typeof(IQueryable<int>), typeof(IQueryable<>));
(Note that this will not help with basic types that implement other interfaces, since it will take a lot more code to verify this, using Type.GetInterfacesrecursively.)
source
share