JamesNK/Newtonsoft.Json · error · ArgumentNullException

'{0}' is not a generic class definition.

Error message

'{0}' is not a generic class definition.

What it means

Thrown by ReflectionUtils.InheritsGenericDefinition when the supplied genericClassDefinition is not a class or not a generic type definition. The argument must be an open generic class such as typeof(List<>); a closed generic class (List<int>) or a non-class type is rejected.

Source

Thrown at Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs:378

            }

            implementingType = null;
            return false;
        }

        public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition)
        {
            return InheritsGenericDefinition(type, genericClassDefinition, out _);
        }

        public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type? implementingType)
        {
            ValidationUtils.ArgumentNotNull(type, nameof(type));
            ValidationUtils.ArgumentNotNull(genericClassDefinition, nameof(genericClassDefinition));

            if (!genericClassDefinition.IsClass() || !genericClassDefinition.IsGenericTypeDefinition())
            {
                throw new ArgumentNullException("'{0}' is not a generic class definition.".FormatWith(CultureInfo.InvariantCulture, genericClassDefinition));
            }

            return InheritsGenericDefinitionInternal(type, genericClassDefinition, out implementingType);
        }

        private static bool InheritsGenericDefinitionInternal(Type type, Type genericClassDefinition, out Type? implementingType)
        {
            Type? currentType = type;
            do
            {
                if (currentType.IsGenericType() && genericClassDefinition == currentType.GetGenericTypeDefinition())
                {
                    implementingType = currentType;
                    return true;
                }

                currentType = currentType.BaseType();
            }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Pass an open generic class definition (e.g. typeof(List<>), typeof(Dictionary<,>)).
  2. Validate with genericClassDefinition.IsClass && genericClassDefinition.IsGenericTypeDefinition before calling.

Example fix

// before
bool inherits = ReflectionUtils.InheritsGenericDefinition(type, typeof(Collection<int>));

// after
bool inherits = ReflectionUtils.InheritsGenericDefinition(type, typeof(Collection<>));
Defensive patterns

Strategy: validation

Validate before calling

if (!genericClassDefinition.IsClass || !genericClassDefinition.IsGenericTypeDefinition)
    throw new ArgumentException("Pass an open generic class definition, e.g. typeof(List<>)");

Type guard

static bool IsOpenGenericClass(Type t) => t.IsClass && t.IsGenericTypeDefinition;

Prevention

When it happens

Trigger: Passing a closed generic class (typeof(List<int>)), a non-generic class, or an interface as the genericClassDefinition argument.

Common situations: Custom contract code probing inheritance against a closed generic, type confusion between interface and class definitions, or refactoring that swapped an open definition for a closed one.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/15086c16705facf9. Report an issue: GitHub.