You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generic attributes: Attribute.GetCustomAttributes returns null when passed an unbound generic type #64335
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<>)) returns null. This is unexpected, particularly as the nullable annotations say that it doesn't return null.
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 from Attribute to Attribute.GetCustomAttributes, you got an exception telling you that. So I suspect this is an unhandled case which just fell through the cracks.
changed the title [-]Attribute.GetCustomAttributes returns null when passed an unbound generic type[/-][+]Generic attributes: Attribute.GetCustomAttributes returns null when passed an unbound generic type[/+]on Jan 26, 2022
element.GetCustomAttributes returns an empty object[] since it is not possible to create array with open generic element type. Casting object[] to Attribute[] using as is producing null.
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 caType.IsAssignableTo(typeof(Attribute)) and if so, set arrayType = (RuntimeType)typeof(Attribute) for the array creation.
Activity
[-]Attribute.GetCustomAttributes returns null when passed an unbound generic type[/-][+]Generic attributes: Attribute.GetCustomAttributes returns null when passed an unbound generic type[/+]VBAndCs commentedon Jan 26, 2022
I have asked to accept the open generic form and return all generic attributes of this type #64169
CyrusNajmabadi commentedon Jan 26, 2022
@VBAndCs you didn't address teh question i asked you there. :)
jkotas commentedon Jan 26, 2022
The bug is caused by
!at this lineruntime/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
Line 486 in 03e0187
element.GetCustomAttributesreturns an emptyobject[]since it is not possible to create array with open generic element type. Castingobject[]toAttribute[]usingasis producingnull.VBAndCs commentedon Jan 26, 2022
@jkotas
You can return an Attrubute array containing all the generic versions of this open generic attribute.
Joe4evr commentedon Jan 31, 2022
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
caType.IsAssignableTo(typeof(Attribute))and if so, setarrayType = (RuntimeType)typeof(Attribute)for the array creation.Effectively:
(and there's a total of 3 places in the file for this.)
madelson commentedon Feb 11, 2022
@Joe4evr / @buyaa-n I'd be interested in fixing this if it is still up for grabs.
buyaa-n commentedon Feb 11, 2022
@Joe4evr are working on the fix? if not i will assign to @madelson, thanks!
Joe4evr commentedon Feb 11, 2022
@buyaa-n I was trying to, but I just ran out of time over the past week, so it's cool.
Avoid Attribute.GetCustomAttributes() returning null for open generic…
Fix GenericAttributeTests to reflect dotnet#64335
Avoid Attribute.GetCustomAttributes() returning null for open generic…
stephentoub commentedon Mar 11, 2022
Reopening as #65237 was reverted in #66508 due to #66496.