{"record":{"id":"147800c1bfe28b4b","repo":"JamesNK/Newtonsoft.Json","slug":"unable-to-find-default-constructor-for","errorCode":null,"errorMessage":"Unable to find default constructor for ","messagePattern":"Unable to find default constructor for ","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs","lineNumber":86,"sourceCode":"            return (o, a) => method.Invoke(o, a);\n        }\n\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public override Func<T> CreateDefaultConstructor<T>(\n            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]\n            Type type)\n        {\n            ValidationUtils.ArgumentNotNull(type, nameof(type));\n\n            if (type.IsValueType())\n            {\n                return () => (T)Activator.CreateInstance(type)!;\n            }\n\n            ConstructorInfo? constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true);\n            if (constructorInfo == null)\n            {\n                throw new InvalidOperationException(\"Unable to find default constructor for \" + type.FullName);\n            }\n\n            return () => (T)constructorInfo.Invoke(null);\n        }\n\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public override Func<T, object?> CreateGet<T>(PropertyInfo propertyInfo)\n        {\n            ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo));\n\n            return o => propertyInfo.GetValue(o, null);\n        }\n\n        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]\n        public override Func<T, object?> CreateGet<T>(FieldInfo fieldInfo)\n        {\n            ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo));\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/LateBoundReflectionDelegateFactory.cs#L68-L104","documentation":"Thrown by LateBoundReflectionDelegateFactory.CreateDefaultConstructor when ReflectionUtils.GetDefaultConstructor(type, true) returns null for a reference type. GetDefaultConstructor is called with nonPublic=true, so this means there is genuinely no parameterless constructor (public or non-public) on the type. The LateBound factory is the fallback path used for structs, abstract types, and WinRT/Win8 edge cases, or when the Expression factory is unavailable.","triggerScenarios":"Deserializing into a class that only declares parameterized constructors and has no parameterless one — and no [JsonConstructor] marks a parameterized constructor. Also triggered by ReflectionObject.Create when no creator method is supplied and the type lacks a default constructor.","commonSituations":"DTOs with only parameterized constructors (DDD-style or record types), types designed for DI containers that never need a default ctor, removing an accidental parameterless ctor during refactoring, or third-party types you cannot modify.","solutions":["Add a parameterless constructor to the type (protected/private is fine since nonPublic=true is passed).","Mark one parameterized constructor with [JsonConstructor] so Json.NET uses it instead of seeking a default ctor.","Use a custom IContractResolver overriding CreateObjectContract to set an OverrideCreator with the desired constructor.","If using ReflectionObject directly, pass an explicit creator MethodBase to ReflectionObject.Create."],"exampleFix":"// before\npublic class Money\n{\n    public Money(decimal amount, string currency) { ... }\n}\n\n// after\npublic class Money\n{\n    public Money(decimal amount, string currency) { ... }\n    [JsonConstructor]\n    public Money() { Amount = 0; Currency = \"USD\"; }\n}","handlingStrategy":"validation","validationCode":"// Verify a parameterless ctor exists before serializing/deserializing.\nbool HasDefaultCtor(Type t) =>\n    t.IsValueType || t.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null) != null;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always provide a parameterless constructor on serialized DTOs, or mark one parameterized ctor with [JsonConstructor].","Add a unit test that deserializes a sample JSON for each DTO to catch missing-ctor issues at build time.","For third-party types, wrap them in a serializable adapter or use a custom IContractResolver."],"tags":["reflection","deserialization","constructor","default-constructor"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}