Skip to content

Generic attributes: Attribute.GetCustomAttributes returns null when passed an unbound generic type #64335

Description

@canton7

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<>)) 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.

Reproduction Steps

Test.M();

public class Test
{
    public static void M()
    {
        var attrs = Attribute.GetCustomAttributes(typeof(Test), typeof(TestAttribute<>));
        Console.WriteLine(attrs == null); // true
    }
}

public class TestAttribute<T> : Attribute { }

SharpLab

Expected behavior

Attribute.GetCustomAttributes either returns an array, or throws an exception. It should not return null.

Actual behavior

Attribute.GetCustomAttributes returns null 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

Activity

  1. ghost added
    untriaged
    on Jan 26, 2022
  2. 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
  3. VBAndCs commented on Jan 26, 2022

    @VBAndCs

    I have asked to accept the open generic form and return all generic attributes of this type #64169

  4. CyrusNajmabadi commented on Jan 26, 2022

    @CyrusNajmabadi
    Contributor

    @VBAndCs you didn't address teh question i asked you there. :)

  5. jkotas commented on Jan 26, 2022

    @jkotas
    Member

    The bug is caused by ! at this line

    _ => (element.GetCustomAttributes(typeof(Attribute), inherit) as Attribute[])!,
    :

    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.

  6. VBAndCs commented on Jan 26, 2022

    @VBAndCs

    @jkotas

    since it is not possible to create array with open generic element type

    You can return an Attrubute array containing all the generic versions of this open generic attribute.

  7. added and removed
    untriaged
    on Jan 28, 2022
  8. added this to the 7.0.0 milestone on Jan 28, 2022
  9. added
    help wanted
    on Jan 28, 2022
  10. Joe4evr commented on Jan 31, 2022

    @Joe4evr
    Contributor

    So I took a look at this, and I think the best solution would be down around here:

    bool useObjectArray = (caType.IsValueType || caType.ContainsGenericParameters);
    RuntimeType arrayType = useObjectArray ? (RuntimeType)typeof(object) : caType;

    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.

    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.)

  11. madelson commented on Feb 11, 2022

    @madelson
    Contributor

    @Joe4evr / @buyaa-n I'd be interested in fixing this if it is still up for grabs.

  12. buyaa-n commented on Feb 11, 2022

    @buyaa-n
    Contributor

    @Joe4evr are working on the fix? if not i will assign to @madelson, thanks!

  13. Joe4evr commented on Feb 11, 2022

    @Joe4evr
    Contributor

    @buyaa-n I was trying to, but I just ran out of time over the past week, so it's cool.

  14. removed
    help wanted
    on Feb 11, 2022
  15. added a commit that references this issue on Feb 12, 2022
    79c3416
  16. ghost added
    in-pr
    on Feb 12, 2022
  17. added a commit that references this issue on Mar 4, 2022
    2029495
  18. ghost removed
    in-pr
    on Mar 4, 2022
  19. stephentoub commented on Mar 11, 2022

    @stephentoub
    Member

    Reopening as #65237 was reverted in #66508 due to #66496.

  20. ghost locked as resolved and limited conversation to collaborators on Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions