Skip to content

Commit 24cb732

Browse files
authored
Fix Type.GetCustomAttributes for multiple instances of the same attribute (dotnet#24474)
Fixes dotnet/corefx#37261
1 parent c4a0253 commit 24cb732

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ internal static object[] GetCustomAttributes(RuntimeType type, RuntimeType caTyp
11591159

11601160
while (type != (RuntimeType)typeof(object) && type != null)
11611161
{
1162-
AddCustomAttributes(ref result, type.GetRuntimeModule(), type.MetadataToken, caType, mustBeInheritable, ref result);
1162+
AddCustomAttributes(ref result, type.GetRuntimeModule(), type.MetadataToken, caType, mustBeInheritable, result);
11631163
mustBeInheritable = true;
11641164
type = (type.BaseType as RuntimeType)!;
11651165
}
@@ -1202,7 +1202,7 @@ internal static object[] GetCustomAttributes(RuntimeMethodInfo method, RuntimeTy
12021202

12031203
while (method != null)
12041204
{
1205-
AddCustomAttributes(ref result, method.GetRuntimeModule(), method.MetadataToken, caType, mustBeInheritable, ref result);
1205+
AddCustomAttributes(ref result, method.GetRuntimeModule(), method.MetadataToken, caType, mustBeInheritable, result);
12061206
mustBeInheritable = true;
12071207
method = method.GetParentDefinition()!;
12081208
}
@@ -1338,9 +1338,8 @@ private static object[] GetCustomAttributes(
13381338
RuntimeModule decoratedModule, int decoratedMetadataToken, int pcaCount, RuntimeType? attributeFilterType)
13391339
{
13401340
RuntimeType.ListBuilder<object> attributes = new RuntimeType.ListBuilder<object>();
1341-
RuntimeType.ListBuilder<object> _ = default;
13421341

1343-
AddCustomAttributes(ref attributes, decoratedModule, decoratedMetadataToken, attributeFilterType, false, ref _);
1342+
AddCustomAttributes(ref attributes, decoratedModule, decoratedMetadataToken, attributeFilterType, false, new RuntimeType.ListBuilder<object>());
13441343

13451344
bool useObjectArray = attributeFilterType == null || attributeFilterType.IsValueType || attributeFilterType.ContainsGenericParameters;
13461345
RuntimeType arrayType = useObjectArray ? (RuntimeType)typeof(object) : attributeFilterType!;
@@ -1356,7 +1355,9 @@ private static object[] GetCustomAttributes(
13561355
private static void AddCustomAttributes(
13571356
ref RuntimeType.ListBuilder<object> attributes,
13581357
RuntimeModule decoratedModule, int decoratedMetadataToken,
1359-
RuntimeType? attributeFilterType, bool mustBeInheritable, ref RuntimeType.ListBuilder<object> derivedAttributes)
1358+
RuntimeType? attributeFilterType, bool mustBeInheritable,
1359+
// The derivedAttributes list must be passed by value so that it is not modified with the discovered attributes
1360+
RuntimeType.ListBuilder<object> derivedAttributes)
13601361
{
13611362
CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken);
13621363

0 commit comments

Comments
 (0)