JamesNK/Newtonsoft.Json · error · Exception
Could not create getter for
Error message
Could not create getter for {0}. What it means
Thrown by ReflectionDelegateFactory.CreateGet(MemberInfo) when the supplied MemberInfo is neither a PropertyInfo nor a FieldInfo. The generic CreateGet only knows how to build value getters for fields and properties; any other member kind (method, event, constructor, nested type) falls through to this exception. It signals that an unexpected member type was fed into the getter pipeline.
Solutions
- Ensure only FieldInfo or PropertyInfo is passed to CreateGet — filter the member set beforehand.
- For methods, use CreateMethodCall instead.
- Audit custom contract resolvers / reflection helpers for the member kinds they hand to the delegate factory.
Example fix
// before
var getter = factory.CreateGet<object>(someMethodInfo);
// after
if (member is PropertyInfo p) getter = factory.CreateGet<object>(p);
else if (member is FieldInfo f) getter = factory.CreateGet<object>(f);
else throw new InvalidOperationException($"Unsupported member: {member}"); Defensive patterns
Strategy: type-guard
Validate before calling
if (member is not PropertyInfo and not FieldInfo) throw new InvalidOperationException($"Cannot get {member.MemberType}");
var getter = factory.CreateGet<object>(member); Type guard
static bool IsGettableMember(MemberInfo m) => m is PropertyInfo or FieldInfo;
Prevention
- Filter MemberInfo collections to PropertyInfo/FieldInfo before invoking the getter factory.
- Use GetProperties()/GetFields() instead of the broad GetMembers().
- In custom contract resolvers, guard member kinds before delegating to the factory.
When it happens
Trigger: Passing a MethodInfo, EventInfo, ConstructorInfo, or TypeInfo into CreateGet. Internally this can occur if a custom contract resolver or member-walking utility mistakenly treats a non-field/non-property member as a value source.
Common situations: Custom IContractResolver implementations, extension methods that iterate MemberInfo collections, or mismatches after upgrading Json.NET where member resolution logic changed.
Related errors
- Could not create setter for
- Error getting value from
- Error getting value from
- Error getting value from
- Expected a single member with the name
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/191172a1c2b93538.
Report an issue: GitHub.
Appendix: source
Thrown at Src/Newtonsoft.Json/Utilities/ReflectionDelegateFactory.cs:59
public Func<T, object?> CreateGet<T>(MemberInfo memberInfo)
{
if (memberInfo is PropertyInfo propertyInfo)
{
// https://github.com/dotnet/corefx/issues/26053
if (propertyInfo.PropertyType.IsByRef)
{
throw new InvalidOperationException("Could not create getter for {0}. ByRef return values are not supported.".FormatWith(CultureInfo.InvariantCulture, propertyInfo));
}
return CreateGet<T>(propertyInfo);
}
if (memberInfo is FieldInfo fieldInfo)
{
return CreateGet<T>(fieldInfo);
}
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));
}
View on GitHub (pinned to 4f73e74372)