-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Generic attributes: Attribute.GetCustomAttributes returns null when passed an unbound generic type #64335
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsDescription
However, It's worth noting that this wasn't previously possible: it was simply not possible to create a generic type which derived from Reproduction Stepsnew Test().M();
public class Test
{
public void M()
{
var attrs = Attribute.GetCustomAttributes(this.GetType(), typeof(TestAttribute<>));
Console.WriteLine(attrs == null); // true
}
}
public class TestAttribute<T> : Attribute { } Expected behavior
Actual behavior
Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
I have asked to accept the open generic form and return all generic attributes of this type #64169 |
@VBAndCs you didn't address teh question i asked you there. :) |
The bug is caused by
|
You can return an Attrubute array containing all the generic versions of this open generic attribute. |
So I took a look at this, and I think the best solution would be down around here: runtime/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs Lines 931 to 932 in 03e0187
While my gut reaction is to possibly rethink some parts of this implementation, there's probably more involved that I can't see and may end up breaking assumptions elsewhere. In which case, the most effective fix here might be to add a check whether or not Effectively: bool useObjectArray = (caType.IsValueType || caType.ContainsGenericParameters);
+ bool useAttributeArray = (caType.ContainsGenericParameters && caType.IsAssignableTo(typeof(Attribute)));
- RuntimeType arrayType = useObjectArray ? (RuntimeType)typeof(object) : caType;
+ RuntimeType arrayType = useAttributeArray ? (RuntimeType)typeof(Attribute) : useObjectArray ? (RuntimeType)typeof(object) : caType; (and there's a total of 3 places in the file for this.) |
@buyaa-n I was trying to, but I just ran out of time over the past week, so it's cool. |
Previously, this test asserted that GetCustomAttributes on an open generic type would return null. Now we return empty instead.
Description
Attribute.GetCustomAttributes
normally returns an empty array when no attributes are found (or throws an exception if the parameters don't make sense).However,
Attribute.GetCustomAttributes(someType, typeof(SomeGenericAttribute<>))
returnsnull
. This is unexpected, particularly as the nullable annotations say that it doesn't returnnull
.It's worth noting that this wasn't previously possible: it was simply not possible to create a generic type which derived from
Attribute
, and if you passed a type which wasn't derived fromAttribute
toAttribute.GetCustomAttributes
, you got an exception telling you that. So I suspect this is an unhandled case which just fell through the cracks.Reproduction Steps
SharpLab
Expected behavior
Attribute.GetCustomAttributes
either returns an array, or throws an exception. It should not returnnull
.Actual behavior
Attribute.GetCustomAttributes
returnsnull
in this case.Regression?
No. It was not previously possible to pass an unbound generic attribute type to
Attribute.GetCustomAttributes
.Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: