C# check if class has generic base type

Date: 2020-02-06
public static bool IsSubclassOfRawGeneric(Type toCheck, Type baseType)
{
	while (toCheck != typeof(object))
	{
		Type cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
		if (baseType == cur)
		{
			return true;
		}
		toCheck = toCheck.BaseType;
	}
	return false;
}
// example:
var baseType = typeof(BaseDataMapper<>);
var mappers = baseType.Assembly.GetTypes().Where(t => t.IsClass && t != baseType && IsSubclassOfRawGeneric(t, baseType));
33590cookie-checkC# check if class has generic base type