{"record":{"id":"7c08d50a84c33a28","repo":"MassTransit/MassTransit","slug":"declaringtype-is-null","errorCode":null,"errorMessage":"DeclaringType is null","messagePattern":"DeclaringType is null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/MassTransit.Abstractions/Internals/Reflection/ReadOnlyProperty.cs","lineNumber":28,"sourceCode":"        public readonly Func<object, object> GetProperty;\n\n        public ReadOnlyProperty(PropertyInfo property)\n        {\n            Property = property;\n            GetProperty = GetGetMethod(Property);\n        }\n\n        public PropertyInfo Property { get; private set; }\n\n        public object Get(object instance)\n        {\n            return GetProperty(instance);\n        }\n\n        static Func<object, object> GetGetMethod(PropertyInfo property)\n        {\n            if (property.DeclaringType == null)\n                throw new ArgumentException(\"DeclaringType is null\", nameof(property));\n            if (property.GetMethod == null)\n                return _ => throw new InvalidOperationException(\"No GetMethod available on \" + property.Name);\n\n            var instance = Expression.Parameter(typeof(object), \"instance\");\n            var instanceCast = property.DeclaringType.IsValueType\n                ? Expression.Convert(instance, property.DeclaringType)\n                : Expression.TypeAs(instance, property.DeclaringType);\n\n            var call = Expression.Call(instanceCast, property.GetMethod);\n            var typeAs = Expression.TypeAs(call, typeof(object));\n\n            return Expression.Lambda<Func<object, object>>(typeAs, instance).Compile();\n        }\n    }\n\n\n    public class ReadOnlyProperty<T>\n    {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/MassTransit/MassTransit/blob/62ab339afa3bac2e9b3fe1769d0d35d7e44778e9/src/MassTransit.Abstractions/Internals/Reflection/ReadOnlyProperty.cs#L10-L46","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar ro = new ReadOnlyProperty(somePropertyInfo); // DeclaringType null\n// after\nif (somePropertyInfo.DeclaringType == null)\n    throw new InvalidOperationException($\"Property {somePropertyInfo.Name} has no declaring type\");\nvar ro = new ReadOnlyProperty(somePropertyInfo);","handlingStrategy":"type-guard","validationCode":"var usable = properties.Where(p => p.DeclaringType != null);\nforeach (var p in usable)\n    builders.Add(new ReadOnlyProperty(p));","typeGuard":"static bool HasDeclaringType(PropertyInfo p) => p.DeclaringType != null;","tryCatchPattern":"try\n{\n    var ro = new ReadOnlyProperty(property);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"property\")\n{\n    logger.LogError(ex, \"Property {Name} has null DeclaringType; not a real type member\", property.Name);\n}","preventionTips":["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"],"tags":["reflection","invalid-argument","masstransit"],"backgroundTag":"null-argument","analyzedSha":"62ab339afa3bac2e9b3fe1769d0d35d7e44778e9","analyzedAt":"2026-09-13T22:47:47.593Z","contentChangedAt":"2026-09-13T22:47:47.593Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}