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
- Verify the PropertyInfo comes from a real type's members (Type.GetProperty / GetProperties return properties with DeclaringType set)
- Filter out properties where DeclaringType == null before constructing ReadOnlyProperty
- 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
- Obtain PropertyInfo only via Type.GetProperty/GetProperties
- Skip synthetic/dynamic members lacking declaring types in member scans
- Assert non-null DeclaringType at the edge of your reflection utilities
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
- The interface type must be an interface:
- The interface type is not implemented by:
- Could not find open type in type:
- The interface must be a generic interface definition: {TypeC
- The type {TypeCache.GetShortName(type)} is not in an assembl
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)