Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono] Fix GetCustomAttributes System.Reflection API with a custom attribute provider #94602

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,113 @@ public void StringAttributeValueRefEqualsStringEmpty () {

Assert.Same(string.Empty, attr.NamedField);
}

[AttributeUsage(AttributeTargets.Parameter)]
internal class MyParameterAttribute : Attribute {}

[AttributeUsage(AttributeTargets.Property)]
internal class MyPropertyAttribute : Attribute {}

internal sealed class PropertyAsParameterInfo : ParameterInfo
{
private readonly PropertyInfo _underlyingProperty;
private readonly ParameterInfo? _constructionParameterInfo;

public PropertyAsParameterInfo(PropertyInfo property, ParameterInfo parameterInfo)
{
_underlyingProperty = property;
_constructionParameterInfo = parameterInfo;
MemberImpl = _underlyingProperty;
}

public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
var constructorAttributes = _constructionParameterInfo?.GetCustomAttributes(attributeType, inherit);

if (constructorAttributes == null || constructorAttributes is { Length: 0 })
{
return _underlyingProperty.GetCustomAttributes(attributeType, inherit);
}

var propertyAttributes = _underlyingProperty.GetCustomAttributes(attributeType, inherit);

var mergedAttributes = new Attribute[constructorAttributes.Length + propertyAttributes.Length];
Array.Copy(constructorAttributes, mergedAttributes, constructorAttributes.Length);
Array.Copy(propertyAttributes, 0, mergedAttributes, constructorAttributes.Length, propertyAttributes.Length);

return mergedAttributes;
}

public override object[] GetCustomAttributes(bool inherit)
{
var constructorAttributes = _constructionParameterInfo?.GetCustomAttributes(inherit);

if (constructorAttributes == null || constructorAttributes is { Length: 0 })
{
return _underlyingProperty.GetCustomAttributes(inherit);
}

var propertyAttributes = _underlyingProperty.GetCustomAttributes(inherit);

var mergedAttributes = new object[constructorAttributes.Length + propertyAttributes.Length];
Array.Copy(constructorAttributes, mergedAttributes, constructorAttributes.Length);
Array.Copy(propertyAttributes, 0, mergedAttributes, constructorAttributes.Length, propertyAttributes.Length);

return mergedAttributes;
}

public override IList<CustomAttributeData> GetCustomAttributesData()
{
var attributes = new List<CustomAttributeData>(
_constructionParameterInfo?.GetCustomAttributesData() ?? Array.Empty<CustomAttributeData>());
attributes.AddRange(_underlyingProperty.GetCustomAttributesData());

return attributes.AsReadOnly();
}
}

internal class CustomAttributeProviderTestClass
{
public CustomAttributeProviderTestClass([MyParameter] int integerProperty)
{
IntegerProperty = integerProperty;
}

[MyProperty]
public int IntegerProperty { get; set; }
}

[Fact]
public void CustomAttributeProvider ()
{
var type = typeof(CustomAttributeProviderTestClass);
var propertyInfo = type.GetProperty(nameof(CustomAttributeProviderTestClass.IntegerProperty));
var ctorInfo = type.GetConstructor(new Type[] { typeof(int) });
var ctorParamInfo = ctorInfo.GetParameters()[0];
var propertyAndParamInfo = new PropertyAsParameterInfo(propertyInfo, ctorParamInfo);

// check GetCustomAttribute API
var cattrObjects = propertyAndParamInfo.GetCustomAttributes(true);
Assert.Equal(2, cattrObjects.Length);
Assert.Equal(typeof(MyParameterAttribute), cattrObjects[0].GetType());
Assert.Equal(typeof(MyPropertyAttribute), cattrObjects[1].GetType());

cattrObjects = propertyAndParamInfo.GetCustomAttributes(typeof(Attribute), true);
Assert.Equal(2, cattrObjects.Length);
Assert.Equal(typeof(MyParameterAttribute), cattrObjects[0].GetType());
Assert.Equal(typeof(MyPropertyAttribute), cattrObjects[1].GetType());

var cattrsEnumerable = propertyAndParamInfo.GetCustomAttributes();
Attribute[] cattrs = cattrsEnumerable.Cast<Attribute>().ToArray();
Assert.Equal(2, cattrs.Length);
Assert.Equal(typeof(MyParameterAttribute), cattrs[0].GetType());
Assert.Equal(typeof(MyPropertyAttribute), cattrs[1].GetType());

// check GetCustomAttributeData API
var customAttributesData = propertyAndParamInfo.GetCustomAttributesData();
Assert.Equal(2, customAttributesData.Count);
Assert.Equal(typeof(MyParameterAttribute), customAttributesData[0].AttributeType);
Assert.Equal(typeof(MyPropertyAttribute), customAttributesData[1].AttributeType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ private static bool IsUserCattrProvider(object obj)
// FIXME: Callers are explicitly passing in null for attributeType, but GetCustomAttributes prohibits null attributeType arguments
internal static object[] GetCustomAttributesBase(ICustomAttributeProvider obj, Type? attributeType, bool inheritedOnly)
{
object[] attrs;

if (IsUserCattrProvider(obj))
attrs = obj.GetCustomAttributes(attributeType!, true);
else
attrs = GetCustomAttributesInternal(obj, attributeType!, false);

object[] attrs = GetCustomAttributesInternal(obj, attributeType!, pseudoAttrs: false);
//
// All pseudo custom attributes are Inherited = false hence we can avoid
// building attributes array which would be discarded by inherited checks
Expand Down Expand Up @@ -156,6 +150,9 @@ internal static object[] GetCustomAttributes(ICustomAttributeProvider obj, Type
&& attributeType != typeof(Attribute) && attributeType != typeof(CustomAttribute) && attributeType != typeof(object))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);

if (IsUserCattrProvider(obj))
return obj.GetCustomAttributes(attributeType, inherit);

// FIXME: GetCustomAttributesBase doesn't like being passed a null attributeType
if (attributeType == typeof(CustomAttribute))
attributeType = null!;
Expand Down Expand Up @@ -306,6 +303,9 @@ internal static object[] GetCustomAttributes(ICustomAttributeProvider obj, bool
{
ArgumentNullException.ThrowIfNull(obj);

if (IsUserCattrProvider(obj))
return obj.GetCustomAttributes(typeof(Attribute), inherit);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am passing in typeof(Attribute) here to match CoreCLR implementation:

public static Attribute[] GetCustomAttributes(ParameterInfo element, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
if (element.Member == null)
throw new ArgumentException(SR.Argument_InvalidParameterInfo, nameof(element));
MemberInfo member = element.Member;
if (member.MemberType == MemberTypes.Method && inherit)
return InternalParamGetCustomAttributes(element, null, inherit);
return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
}


if (!inherit)
return (object[])GetCustomAttributesBase(obj, null, false).Clone();

Expand Down
Loading