MassTransit/MassTransit · error · ArgumentException

DeclaringType is null

Error message

DeclaringType is null

What it means

ReadOnlyProperty.GetGetMethod compiles a getter delegate for a PropertyInfo and throws this ArgumentException when property.DeclaringType is null. A null DeclaringType means the property did not come from a concrete attached type (e.g. a non-member or dynamically synthesized property), so an expression tree getter cannot be built against a target type. The parameter name (property) is attached to the exception for diagnostics.

Solutions

  1. Verify the PropertyInfo comes from a real type's members (Type.GetProperty / GetProperties return properties with DeclaringType set)
  2. Filter out properties where DeclaringType == null before constructing ReadOnlyProperty
  3. If wrapping PropertyInfo, store and use the owner type explicitly instead of relying on DeclaringType

Example fix

// before
var ro = new ReadOnlyProperty(somePropertyInfo); // DeclaringType null
// after
if (somePropertyInfo.DeclaringType == null)
    throw new InvalidOperationException($"Property {somePropertyInfo.Name} has no declaring type");
var ro = new ReadOnlyProperty(somePropertyInfo);
Defensive patterns

Strategy: type-guard

Validate before calling

var usable = properties.Where(p => p.DeclaringType != null);
foreach (var p in usable)
    builders.Add(new ReadOnlyProperty(p));

Type guard

static bool HasDeclaringType(PropertyInfo p) => p.DeclaringType != null;

Try / catch

try
{
    var ro = new ReadOnlyProperty(property);
}
catch (ArgumentException ex) when (ex.ParamName == "property")
{
    logger.LogError(ex, "Property {Name} has null DeclaringType; not a real type member", property.Name);
}

Prevention

When it happens

Trigger: Constructing a ReadOnlyProperty (via MassTransit reflection helpers) from a PropertyInfo whose DeclaringType is null — e.g. a property obtained from unusual dynamic scenarios or a manually fabricated PropertyInfo.

Common situations: Reflection over dynamic/emit-generated members without a declaring type; custom property abstractions wrapping non-standard PropertyInfos; serialization helpers iterating members where some members lack declaring types.

Related errors


AI-assisted analysis of MassTransit/MassTransit@62ab339afa (2026-09-13). Data as JSON: /api/errors/7c08d50a84c33a28. Report an issue: GitHub.

Appendix: source

Thrown at src/MassTransit.Abstractions/Internals/Reflection/ReadOnlyProperty.cs:28

        public readonly Func<object, object> GetProperty;

        public ReadOnlyProperty(PropertyInfo property)
        {
            Property = property;
            GetProperty = GetGetMethod(Property);
        }

        public PropertyInfo Property { get; private set; }

        public object Get(object instance)
        {
            return GetProperty(instance);
        }

        static Func<object, object> GetGetMethod(PropertyInfo property)
        {
            if (property.DeclaringType == null)
                throw new ArgumentException("DeclaringType is null", nameof(property));
            if (property.GetMethod == null)
                return _ => throw new InvalidOperationException("No GetMethod available on " + property.Name);

            var instance = Expression.Parameter(typeof(object), "instance");
            var instanceCast = property.DeclaringType.IsValueType
                ? Expression.Convert(instance, property.DeclaringType)
                : Expression.TypeAs(instance, property.DeclaringType);

            var call = Expression.Call(instanceCast, property.GetMethod);
            var typeAs = Expression.TypeAs(call, typeof(object));

            return Expression.Lambda<Func<object, object>>(typeAs, instance).Compile();
        }
    }


    public class ReadOnlyProperty<T>
    {

View on GitHub (pinned to 62ab339afa)