JamesNK/Newtonsoft.Json · error · ArgumentException

MemberInfo must be of type FieldInfo, PropertyInfo, EventInf

Error message

MemberInfo must be of type FieldInfo, PropertyInfo, EventInfo or MethodInfo

What it means

Thrown by ReflectionUtils.GetMemberUnderlyingType's default case when a MemberInfo is not a Field, Property, Event, or Method. Only those four member kinds carry a meaningful underlying type that this utility knows how to extract; constructors, nested types, and TypeInfo fall through to this exception.

Source

Thrown at Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs:486

        /// </summary>
        /// <param name="member">The member.</param>
        /// <returns>The underlying type of the member.</returns>
        public static Type GetMemberUnderlyingType(MemberInfo member)
        {
            ValidationUtils.ArgumentNotNull(member, nameof(member));

            switch (member.MemberType())
            {
                case MemberTypes.Field:
                    return ((FieldInfo)member).FieldType;
                case MemberTypes.Property:
                    return ((PropertyInfo)member).PropertyType;
                case MemberTypes.Event:
                    return ((EventInfo)member).EventHandlerType!;
                case MemberTypes.Method:
                    return ((MethodInfo)member).ReturnType;
                default:
                    throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo, EventInfo or MethodInfo", nameof(member));
            }
        }

        public static bool IsByRefLikeType(Type type)
        {
            if (!type.IsValueType())
            {
                return false;
            }

            // IsByRefLike flag on type is not available in netstandard2.0
            Attribute[] attributes = GetAttributes(type, null, false);
            for (int i = 0; i < attributes.Length; i++)
            {
                if (string.Equals(attributes[i].GetType().FullName, "System.Runtime.CompilerServices.IsByRefLikeAttribute", StringComparison.Ordinal))
                {
                    return true;
                }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Filter to Field/Property/Event/Method before calling GetMemberUnderlyingType.
  2. Handle other member kinds (nested types, constructors) explicitly in your code.
  3. Prefer GetFields()/GetProperties() over the broad GetMembers() when iterating.

Example fix

// before
foreach (MemberInfo m in type.GetMembers())
    types.Add(ReflectionUtils.GetMemberUnderlyingType(m));

// after
foreach (MemberInfo m in type.GetMembers())
    if (m is FieldInfo or PropertyInfo or EventInfo or MethodInfo)
        types.Add(ReflectionUtils.GetMemberUnderlyingType(m));
Defensive patterns

Strategy: type-guard

Validate before calling

if (member is not FieldInfo and not PropertyInfo and not EventInfo and not MethodInfo)
    throw new ArgumentException($"Unsupported member type: {member.MemberType}");

Type guard

static bool HasUnderlyingType(MemberInfo m) => m is FieldInfo or PropertyInfo or EventInfo or MethodInfo;

Prevention

When it happens

Trigger: Calling GetMemberUnderlyingType with a ConstructorInfo, a nested TypeInfo, or any member whose MemberType is not Field/Property/Event/Method.

Common situations: Custom code that walks arbitrary MemberInfo collections (e.g. GetMembers()) without filtering by MemberType, or reflection helpers that assume all members are fields/properties.

Related errors


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