Skip to content

Commit f4f3be6

Browse files
authored
Fix System.Object.GetCustomAttributes() returning extra internal attributes on Mono (#117864)
* Fix System.Object GetCustomAttributes inherit parameter inconsistency * Use do-while loop to include System.Object attributes
1 parent b5d37d1 commit f4f3be6

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,12 +1270,12 @@ internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caTyp
12701270
for (int i = 0; i < pcas.Count; i++)
12711271
result.Add(pcas[i]);
12721272

1273-
while (type != (RuntimeType)typeof(object) && type != null)
1273+
do
12741274
{
12751275
AddCustomAttributes(ref result, type.GetRuntimeModule(), type.MetadataToken, caType, mustBeInheritable, result);
12761276
mustBeInheritable = true;
12771277
type = (type.BaseType as RuntimeType)!;
1278-
}
1278+
} while (type != (RuntimeType)typeof(object) && type != null);
12791279

12801280
object[] typedResult = CreateAttributeArrayHelper(caType, result.Count);
12811281
for (int i = 0; i < result.Count; i++)

src/libraries/System.Runtime/tests/System.Reflection.Tests/CustomAttributeTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,14 @@ public void BoxedEnumAttributes()
275275
Assert.Equal(typeof(EnumForArrays[]), att.B3.GetType());
276276
Assert.Equal(EnumForArrays.Two, ((EnumForArrays[])att.B3)[0]);
277277
}
278+
279+
[Fact]
280+
public void SystemObject_GetCustomAttributes_InheritanceConsistency()
281+
{
282+
Type objectType = typeof(object);
283+
object[] attributesWithoutInherit = objectType.GetCustomAttributes(inherit: false);
284+
object[] attributesWithInherit = objectType.GetCustomAttributes(inherit: true);
285+
Assert.Equal(attributesWithoutInherit.Length, attributesWithInherit.Length);
286+
}
278287
}
279288
}

0 commit comments

Comments
 (0)