Skip to content

Commit 1be8fd8

Browse files
authored
Ignore inherit param for ParameterInfo.GetCustomAttributes (#57769)
It is documented that the inherit flag is ignored. Attribute.GetCustomAttributes is to be used to search inheritance chain. This change is only for the Mono classlib implementation. CoreCLR already has this behavior. Adding a test to verify behavior.
1 parent 6cc686f commit 1be8fd8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/libraries/System.Reflection/tests/ParameterInfoTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,22 @@ public void CustomAttributesTest(Type attrType)
229229
Assert.True(prov.IsDefined(attrType, true));
230230
}
231231

232+
[Theory]
233+
[InlineData(0, true, 30)]
234+
[InlineData(1, false, -1)]
235+
[InlineData(2, true, 50)]
236+
public void CustomAttributesInheritanceTest(int paramIndex, bool exists, int expectedMyAttributeValue)
237+
{
238+
ParameterInfo parameterInfo = GetParameterInfo(typeof(DerivedParameterInfoMetadata), "VirtualMethodWithCustomAttributes", paramIndex);
239+
CustomAttributeData attribute = parameterInfo.CustomAttributes.SingleOrDefault(a => a.AttributeType.Equals(typeof(MyAttribute)));
240+
Assert.Equal(exists, attribute != null);
241+
242+
ICustomAttributeProvider prov = parameterInfo as ICustomAttributeProvider;
243+
MyAttribute myAttribute = prov.GetCustomAttributes(typeof(MyAttribute), true).SingleOrDefault() as MyAttribute;
244+
Assert.Equal(exists, myAttribute != null);
245+
Assert.Equal(expectedMyAttributeValue, exists ? myAttribute.Value : expectedMyAttributeValue);
246+
}
247+
232248
[Fact]
233249
public void VerifyGetCustomAttributesData()
234250
{
@@ -345,6 +361,7 @@ public void Foo2([CustomBindingFlags(Value = BindingFlags.IgnoreCase)] BindingFl
345361
public void Foo3([CustomBindingFlags(Value = BindingFlags.DeclaredOnly)] BindingFlags bf = BindingFlags.FlattenHierarchy ) { }
346362

347363
public void MethodWithCustomAttribute([My(2)]string str, int iValue, long lValue) { }
364+
public virtual void VirtualMethodWithCustomAttributes([My(3)]int val1, [My(4)]int val2, int val3) { }
348365

349366
public void Method1(string str, int iValue, long lValue) { }
350367
public void Method2() { }
@@ -372,6 +389,11 @@ public void MethodWithNullableEnum(AttributeTargets? arg = AttributeTargets.All)
372389
public int MethodWithOptionalDefaultOutInMarshalParam([MarshalAs(UnmanagedType.LPWStr)][Out][In] string str = "") { return 1; }
373390
}
374391

392+
public class DerivedParameterInfoMetadata : ParameterInfoMetadata
393+
{
394+
override public void VirtualMethodWithCustomAttributes([My(30)]int val1, int val2, [My(50)]int val3) { }
395+
}
396+
375397
public class GenericClass<T>
376398
{
377399
public void GenericMethod(T t) { }
@@ -380,7 +402,8 @@ public void GenericMethod(T t) { }
380402

381403
private class MyAttribute : Attribute
382404
{
383-
internal MyAttribute(int i) { }
405+
public int Value {get; private set;}
406+
internal MyAttribute(int i) { Value = i;}
384407
}
385408

386409
internal sealed class CustomBindingFlagsAttribute : UsableCustomConstantAttribute

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,20 @@ int MetadataToken
181181
override
182182
object[] GetCustomAttributes(bool inherit)
183183
{
184-
return CustomAttribute.GetCustomAttributes(this, inherit);
184+
// It is documented that the inherit flag is ignored.
185+
// Attribute.GetCustomAttributes is to be used to search
186+
// inheritance chain.
187+
return CustomAttribute.GetCustomAttributes(this, false);
185188
}
186189

187190
public
188191
override
189192
object[] GetCustomAttributes(Type attributeType, bool inherit)
190193
{
191-
return CustomAttribute.GetCustomAttributes(this, attributeType, inherit);
194+
// It is documented that the inherit flag is ignored.
195+
// Attribute.GetCustomAttributes is to be used to search
196+
// inheritance chain.
197+
return CustomAttribute.GetCustomAttributes(this, attributeType, false);
192198
}
193199

194200
internal static object? GetDefaultValueImpl(ParameterInfo pinfo)

0 commit comments

Comments
 (0)