JamesNK/Newtonsoft.Json · error · ArgumentException
Could not get constructor for {0}.
Error message
Could not get constructor for {0}. What it means
GenerateCreateDefaultConstructorIL (DynamicReflectionDelegateFactory.cs:263-294) builds a delegate that news up an instance of `type`. For reference types it resolves a parameterless constructor (public or non-public) via GetConstructor; if none exists it throws ArgumentException at DynamicReflectionDelegateFactory.cs:287. Value types don't hit this (they use InitObj).
Source
Thrown at Src/Newtonsoft.Json/Utilities/DynamicReflectionDelegateFactory.cs:287
if (type.IsValueType())
{
generator.DeclareLocal(type);
generator.Emit(OpCodes.Ldloc_0);
// only need to box if the delegate isn't returning the value type
if (type != delegateType)
{
generator.Emit(OpCodes.Box, type);
}
}
else
{
ConstructorInfo? constructorInfo =
type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, ReflectionUtils.EmptyTypes, null);
if (constructorInfo == null)
{
throw new ArgumentException("Could not get constructor for {0}.".FormatWith(CultureInfo.InvariantCulture, type));
}
generator.Emit(OpCodes.Newobj, constructorInfo);
}
generator.Return();
}
[RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
public override Func<T, object?> CreateGet<T>(PropertyInfo propertyInfo)
{
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + propertyInfo.Name, typeof(object), new[] { typeof(T) }, propertyInfo.DeclaringType!);
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateGetPropertyIL(propertyInfo, generator);
return (Func<T, object?>)dynamicMethod.CreateDelegate(typeof(Func<T, object?>));
}View on GitHub (pinned to 4f73e74372)
Solutions
- Add a parameterless constructor to the type (public or accessible non-public).
- Annotate the constructor you want used with [JsonConstructor].
- Register a custom JsonConverter whose ReadJson constructs the type from its arguments.
- Configure an IContractResolver that supplies a custom ObjectFactory/default-creator.
- If trimming/AOT, ensure the constructor is preserved via DynamicallyAccessedMembers.
Example fix
// before
public class Foo { public Foo(int x) { X = x; } public int X { get; } }
// after
public class Foo {
[JsonConstructor] public Foo(int x) { X = x; }
public Foo() {}
public int X { get; set; }
} Defensive patterns
Strategy: validation
Validate before calling
// Confirm a parameterless constructor (or [JsonConstructor]) exists before serializing.
static bool HasDefaultCtor(Type t) {
if (t.IsValueType) return true;
return t.GetConstructor(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, Type.EmptyTypes, null) != null;
} Type guard
static bool NeedsCtorHelp(Type t) => !t.IsAbstract && !HasDefaultCtor(t) && !t.GetConstructors().Any(c => c.GetCustomAttribute<JsonConstructorAttribute>() != null);
Try / catch
try { JsonConvert.DeserializeObject<T>(json); } catch (ArgumentException ex) when (ex.Message.Contains("Could not get constructor")) { /* add [JsonConstructor] or a parameterless ctor */ } Prevention
- Ensure reference types have a parameterless constructor or a [JsonConstructor].
- Register a custom JsonConverter for immutable types.
- For trimmed/AOT builds, preserve constructors via DynamicallyAccessedMembers.
When it happens
Trigger: The serializer tries to create an instance of a reference type that has no parameterless constructor, and no [JsonConstructor]/ObjectConstructor has been configured to use a parameterized one.
Common situations: DTOs with only parameterized constructors; immutable record-like types; classes whose only ctor is non-public and accessibility settings exclude it; AOT/trimmed scenarios where the ctor was stripped.
Related errors
- Property '{0}' does not have a getter.
- Property does not have a getter.
- Error setting value to '{0}' on '{1}'.
- Error getting value from '{0}' on '{1}'.
- Error setting value to '{0}' on '{1}'.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/4911131a4ccc19a2.
Report an issue: GitHub.