JamesNK/Newtonsoft.Json · error · ArgumentNullException

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

Error message

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

What it means

Thrown by ReflectionUtils.ImplementsGenericDefinition when the supplied genericInterfaceDefinition is not an interface or not a generic type definition. The argument must be an open (unbound) generic interface such as typeof(IEnumerable<>); passing a closed generic interface (IEnumerable<int>) or a non-interface type fails this guard. Note the exception is declared as ArgumentNullException, which is unusual.

Source

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

        public static bool ImplementsGenericDefinition(
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
            Type type,
            Type genericInterfaceDefinition)
        {
            return ImplementsGenericDefinition(type, genericInterfaceDefinition, out _);
        }

        public static bool ImplementsGenericDefinition(
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type,
            Type genericInterfaceDefinition,
            [NotNullWhen(true)]out Type? implementingType)
        {
            ValidationUtils.ArgumentNotNull(type, nameof(type));
            ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, nameof(genericInterfaceDefinition));

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

            if (type.IsInterface())
            {
                if (type.IsGenericType())
                {
                    Type interfaceDefinition = type.GetGenericTypeDefinition();

                    if (genericInterfaceDefinition == interfaceDefinition)
                    {
                        implementingType = type;
                        return true;
                    }
                }
            }

            foreach (Type i in type.GetInterfaces())
            {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Pass the open generic interface definition (e.g. typeof(IEnumerable<>), typeof(IDictionary<,>)).
  2. Validate with genericInterfaceDefinition.IsInterface && genericInterfaceDefinition.IsGenericTypeDefinition before calling.

Example fix

// before
bool isDict = ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary<string, object>));

// after
bool isDict = ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary<,>));
Defensive patterns

Strategy: validation

Validate before calling

if (!genericInterfaceDefinition.IsInterface || !genericInterfaceDefinition.IsGenericTypeDefinition)
    throw new ArgumentException("Pass an open generic interface definition, e.g. typeof(IEnumerable<>)");

Type guard

static bool IsOpenGenericInterface(Type t) => t.IsInterface && t.IsGenericTypeDefinition;

Prevention

When it happens

Trigger: Passing a closed generic interface (typeof(IEnumerable<int>)), a non-generic interface (typeof(IList)), or a non-interface type (typeof(List<>)) as the genericInterfaceDefinition argument.

Common situations: Custom converters or contract resolvers that call ImplementsGenericDefinition with the wrong 'definition' type; copy-paste of closed generics; library version changes that altered which generic definitions are probed.

Related errors


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