{"record":{"id":"2da6492f3b0fa1a2","repo":"JamesNK/Newtonsoft.Json","slug":"0-is-not-a-generic-interface-definition","errorCode":null,"errorMessage":"'{0}' is not a generic interface definition.","messagePattern":"'(.+?)' is not a generic interface definition\\.","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs","lineNumber":331,"sourceCode":"        public static bool ImplementsGenericDefinition(\n            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]\n            Type type,\n            Type genericInterfaceDefinition)\n        {\n            return ImplementsGenericDefinition(type, genericInterfaceDefinition, out _);\n        }\n\n        public static bool ImplementsGenericDefinition(\n            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type,\n            Type genericInterfaceDefinition,\n            [NotNullWhen(true)]out Type? implementingType)\n        {\n            ValidationUtils.ArgumentNotNull(type, nameof(type));\n            ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, nameof(genericInterfaceDefinition));\n\n            if (!genericInterfaceDefinition.IsInterface() || !genericInterfaceDefinition.IsGenericTypeDefinition())\n            {\n                throw new ArgumentNullException(\"'{0}' is not a generic interface definition.\".FormatWith(CultureInfo.InvariantCulture, genericInterfaceDefinition));\n            }\n\n            if (type.IsInterface())\n            {\n                if (type.IsGenericType())\n                {\n                    Type interfaceDefinition = type.GetGenericTypeDefinition();\n\n                    if (genericInterfaceDefinition == interfaceDefinition)\n                    {\n                        implementingType = type;\n                        return true;\n                    }\n                }\n            }\n\n            foreach (Type i in type.GetInterfaces())\n            {","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs#L313-L349","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the open generic interface definition (e.g. typeof(IEnumerable<>), typeof(IDictionary<,>)).","Validate with genericInterfaceDefinition.IsInterface && genericInterfaceDefinition.IsGenericTypeDefinition before calling."],"exampleFix":"// before\nbool isDict = ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary<string, object>));\n\n// after\nbool isDict = ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary<,>));","handlingStrategy":"validation","validationCode":"if (!genericInterfaceDefinition.IsInterface || !genericInterfaceDefinition.IsGenericTypeDefinition)\n    throw new ArgumentException(\"Pass an open generic interface definition, e.g. typeof(IEnumerable<>)\");","typeGuard":"static bool IsOpenGenericInterface(Type t) => t.IsInterface && t.IsGenericTypeDefinition;","tryCatchPattern":null,"preventionTips":["Always pass open generic interfaces (typeof(IEnumerable<>), typeof(IDictionary<,>)) to ImplementsGenericDefinition.","Encapsulate the definition choice in a helper that validates IsInterface && IsGenericTypeDefinition.","Add a unit test covering the generic-definition helpers with sample definitions."],"tags":["reflection","generics","interface","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}