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

  1. Add a parameterless constructor to the type (public or accessible non-public).
  2. Annotate the constructor you want used with [JsonConstructor].
  3. Register a custom JsonConverter whose ReadJson constructs the type from its arguments.
  4. Configure an IContractResolver that supplies a custom ObjectFactory/default-creator.
  5. 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

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


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