diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs index eab9a97e8ba8e4..f92173602ebca0 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs @@ -1270,12 +1270,12 @@ internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caTyp for (int i = 0; i < pcas.Count; i++) result.Add(pcas[i]); - while (type != (RuntimeType)typeof(object) && type != null) + do { AddCustomAttributes(ref result, type.GetRuntimeModule(), type.MetadataToken, caType, mustBeInheritable, result); mustBeInheritable = true; type = (type.BaseType as RuntimeType)!; - } + } while (type != (RuntimeType)typeof(object) && type != null); object[] typedResult = CreateAttributeArrayHelper(caType, result.Count); for (int i = 0; i < result.Count; i++) diff --git a/src/libraries/System.Runtime/tests/System.Reflection.Tests/CustomAttributeTests.cs b/src/libraries/System.Runtime/tests/System.Reflection.Tests/CustomAttributeTests.cs index c3c7f3c465e413..e850c30f723a14 100644 --- a/src/libraries/System.Runtime/tests/System.Reflection.Tests/CustomAttributeTests.cs +++ b/src/libraries/System.Runtime/tests/System.Reflection.Tests/CustomAttributeTests.cs @@ -275,5 +275,14 @@ public void BoxedEnumAttributes() Assert.Equal(typeof(EnumForArrays[]), att.B3.GetType()); Assert.Equal(EnumForArrays.Two, ((EnumForArrays[])att.B3)[0]); } + + [Fact] + public void SystemObject_GetCustomAttributes_InheritanceConsistency() + { + Type objectType = typeof(object); + object[] attributesWithoutInherit = objectType.GetCustomAttributes(inherit: false); + object[] attributesWithInherit = objectType.GetCustomAttributes(inherit: true); + Assert.Equal(attributesWithoutInherit.Length, attributesWithInherit.Length); + } } }