{"record":{"id":"ae49da9b37016bbd","repo":"JamesNK/Newtonsoft.Json","slug":"could-not-find-type-0-in-assembly-1","errorCode":null,"errorMessage":"Could not find type '{0}' in assembly '{1}'.","messagePattern":"Could not find type '(.+?)' in assembly '(.+?)'\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs","lineNumber":115,"sourceCode":"                if (assembly == null)\n                {\n                    throw new JsonSerializationException(\"Could not load assembly '{0}'.\".FormatWith(CultureInfo.InvariantCulture, assemblyName));\n                }\n\n                Type? type = assembly.GetType(typeName);\n                if (type == null)\n                {\n                    // if generic type, try manually parsing the type arguments for the case of dynamically loaded assemblies\n                    // example generic typeName format: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\n                    if (StringUtils.IndexOf(typeName, '`') >= 0)\n                    {\n                        try\n                        {\n                            type = GetGenericTypeFromTypeName(typeName, assembly);\n                        }\n                        catch (Exception ex)\n                        {\n                            throw new JsonSerializationException(\"Could not find type '{0}' in assembly '{1}'.\".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName), ex);\n                        }\n                    }\n\n                    if (type == null)\n                    {\n                        throw new JsonSerializationException(\"Could not find type '{0}' in assembly '{1}'.\".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));\n                    }\n                }\n\n                return type;\n            }\n            else\n            {\n                return Type.GetType(typeName)!;\n            }\n        }\n\n        private Type? GetGenericTypeFromTypeName(string typeName, Assembly assembly)","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/DefaultSerializationBinder.cs#L97-L133","documentation":"The assembly named in $type loaded successfully, but the type name could not be resolved. For generic type names (containing a backtick), Json.NET attempts manual parsing of the generic arguments via GetGenericTypeFromTypeName; if that throws, the exception is wrapped and rethrown as a JsonSerializationException (line 115). This indicates a malformed generic $type string or a generic argument whose own assembly/type is unavailable.","triggerScenarios":"Deserializing a $type like \"System.Collections.Generic.Dictionary`2[[...missing...],[...]]\" whose generic argument string is malformed or references an unloaded assembly/type.","commonSituations":"Cross-version generic type names where an argument's assembly strong-name no longer matches, or hand-crafted/corrupted JSON $type values.","solutions":["Validate/sanitize the $type string before deserialization; reject payloads with malformed generic type names.","Use a custom SerializationBinder to normalize known generic type names to the runtime types available.","Ensure all assemblies referenced by the generic arguments are loaded.","Avoid TypeNameHandling.All/Auto for untrusted or cross-version JSON."],"exampleFix":"// before\nvar obj = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });\n// json has $type = \"System.Collections.Generic.Dictionary`2[[Bad.Arg, NoAssembly],[System.String, mscorlib]]\"\n\n// after\nvar settings = new JsonSerializerSettings {\n    TypeNameHandling = TypeNameHandling.Auto,\n    SerializationBinder = new CustomBinder()\n};\n// CustomBinder.BindToType parses typeName defensively and rejects/remaps unknown generics","handlingStrategy":"validation","validationCode":"public sealed class StrictBinder : DefaultSerializationBinder {\n    public override Type BindToType(string assemblyName, string typeName) {\n        if (typeName.IndexOf('`') >= 0)\n        {\n            // only allow a curated set of generic definitions\n            var allowed = new[] { typeof(List<>), typeof(Dictionary<,>) };\n            // parse defensively; reject anything not on the allowlist\n        }\n        return base.BindToType(assemblyName, typeName);\n    }\n}","typeGuard":"static bool IsWellFormedGenericTypeName(string typeName)\n{\n    var bt = typeName.IndexOf('`');\n    if (bt < 0) return true;\n    return typeName.IndexOf('[', bt) >= 0 && typeName.EndsWith(\"]\");\n}","tryCatchPattern":"try { return JsonConvert.DeserializeObject(json, type, settings); }\ncatch (JsonSerializationException ex) when (ex.Message.StartsWith(\"Could not find type\") && ex.InnerException != null)\n{\n    // generic type parse failed; reject payload or remap via binder\n}","preventionTips":["Validate $type strings before honoring them; reject malformed generic names.","Use a SerializationBinder allowlist that only permits known generic definitions.","Avoid TypeNameHandling on untrusted or cross-version JSON."],"tags":["deserialization","type-name-handling","generic-type","serialization-binder","security"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}