JamesNK/Newtonsoft.Json · error · Exception

Could not create setter for {0}.

Error message

Could not create setter for {0}.

What it means

Thrown by ReflectionDelegateFactory.CreateSet(MemberInfo) when the MemberInfo is neither a PropertyInfo nor a FieldInfo. The setter delegate pipeline only supports fields and properties; methods, events, and other member kinds have no value-assignment semantics in this factory.

Source

Thrown at Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs:75

            }

            throw new Exception("Could not create getter for {0}.".FormatWith(CultureInfo.InvariantCulture, memberInfo));
        }

        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public Action<T, object?> CreateSet<T>(MemberInfo memberInfo)
        {
            if (memberInfo is PropertyInfo propertyInfo)
            {
                return CreateSet<T>(propertyInfo);
            }

            if (memberInfo is FieldInfo fieldInfo)
            {
                return CreateSet<T>(fieldInfo);
            }

            throw new Exception("Could not create setter for {0}.".FormatWith(CultureInfo.InvariantCulture, memberInfo));
        }

        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract MethodCall<T, object?> CreateMethodCall<T>(MethodBase method);
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract ObjectConstructor<object> CreateParameterizedConstructor(MethodBase method);
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract Func<T> CreateDefaultConstructor<T>(
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
            Type type);
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract Func<T, object?> CreateGet<T>(PropertyInfo propertyInfo);
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract Func<T, object?> CreateGet<T>(FieldInfo fieldInfo);
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract Action<T, object?> CreateSet<T>(FieldInfo fieldInfo);
        [RequiresDynamicCode(MiscellaneousUtils.AotWarning)]
        public abstract Action<T, object?> CreateSet<T>(PropertyInfo propertyInfo);

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Ensure only FieldInfo or PropertyInfo is passed to CreateSet.
  2. Filter the member set to settable kinds before invoking the factory.
  3. Review custom member-resolution code that feeds the setter pipeline.

Example fix

// before
var setter = factory.CreateSet<object>(anyMember);

// after
switch (member)
{
    case PropertyInfo pi: setter = factory.CreateSet<object>(pi); break;
    case FieldInfo fi:    setter = factory.CreateSet<object>(fi); break;
    default: throw new InvalidOperationException($"Cannot set {member.MemberType}");
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (member is not PropertyInfo and not FieldInfo) throw new InvalidOperationException($"Cannot set {member.MemberType}");
var setter = factory.CreateSet<object>(member);

Type guard

static bool IsSettableMember(MemberInfo m) => m is PropertyInfo or FieldInfo;

Prevention

When it happens

Trigger: Passing a MethodInfo, EventInfo, or other MemberInfo into CreateSet. Can happen in custom contract resolvers that incorrectly route non-settable members as assignment targets.

Common situations: Custom contract resolvers that misclassify methods as settable, or reflection utilities that walk all members without filtering by kind.

Related errors


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