Skip to content

[API Proposal]: Get info about generic attributes #64169

Closed

Description

@VBAndCs

Background and motivation

Make it easier to handle generic attributes.

API Proposal

  • Allow use open generic as a param to Attribute.GetCustomAttributes Method.. It currently returns an empty array.
  • Add an the instance property TypeArgs to the Attribute class that returns all type args if any.

API Usage

var Attrs = Attribute.GetCustomAttributes(this.GetType(), typeof(TestAttribute<>));
var type = Attrs[0].TypeArgs[0];

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Jan 23, 2022
  2. ghost added
    untriaged
    on Jan 23, 2022
  3. CyrusNajmabadi commented on Jan 24, 2022

    @CyrusNajmabadi
    Contributor

    Add an the instance property TypeArgs to the Attribute class that returns all type args if any.

    I'm not certain why this is needed. Why not just do .GetType().GenericTypeArguments?

  4. buyaa-n commented on Jan 28, 2022

    @buyaa-n
    Contributor

    For adding a new property i agree with @CyrusNajmabadi, doesn't seem we need to add property for TypeArgs

    Allow use open generic as a param to Attribute.GetCustomAttributes Method.. It currently returns a null.

    Makes sense to me, will discuss with the team. As this is not a new API suggestion and we don't need the other API suggestion removing the api-suggestion label

  5. removed
    api-suggestion
    untriaged
    on Jan 28, 2022
  6. added this to the 7.0.0 milestone on Feb 2, 2022
  7. added
    help wanted
    on Feb 11, 2022
  8. madelson commented on Feb 12, 2022

    @madelson
    Contributor

    Just to clarify, is the proposal that calling any GetCustomAttributes() method with an open generic type deriving from Attribute will return all attributes which are or derive from generic instantiations of that type? So if we had:

    class GenericAttribute<T> : Attribute { }
    class DerivesFromGenericAttribute : GenericAttribute<string> { }
    
    [GenericAttribute<int>, GenericAttribute<string>, DerivesFromGenericAttribute]
    class HasAttributes { }
    

    Then typeof(HasAttributes).GetCustomAttributes(typeof(GenericAttribute<>)) would return all 3 attributes?

    @buyaa-n I'm potentially interested in working on this but I'd want to be sure of the expected behavior first.

  9. steveharter commented on Feb 15, 2022

    @steveharter
    Contributor

    Then typeof(HasAttributes).GetCustomAttributes(typeof(GenericAttribute<>)) would return all 3 attributes?

    That is my understanding of the ask. I think it makes sense, although I don't know where this "open generic" convention is used elsewhere for similar filtering or scoping.

    Since the workaround is not that difficult (get all custom attributes and manually filter) this feature is lower-priority.

  10. added
    Priority:3
    on Feb 15, 2022
  11. steveharter commented on Feb 15, 2022

    @steveharter
    Contributor

    @GrabYourPitchforks if we add a new convention for open generic types to be used to mean all closed, does this rise to the level of discussing it in an API review meeting?

  12. buyaa-n commented on Feb 16, 2022

    @buyaa-n
    Contributor

    Then typeof(HasAttributes).GetCustomAttributes(typeof(GenericAttribute<>)) would return all 3 attributes?

    I believe that should depend on if the bool inherit parameter set, as it is false by default, in this case DerivesFromGenericAttribute should not be included.

  13. 1 remaining item

  14. added
    api-ready-for-review
    and removed
    help wanted
    on Apr 13, 2022
  15. terrajobst commented on Apr 14, 2022

    @terrajobst
    Contributor

    Yes, reflection should support retrieving attribute by open generic types. And yes, using a base type should also work, assuming inherit is set to true.

  16. added
    api-approved
    and removed
    api-ready-for-review
    on Apr 14, 2022
  17. buyaa-n commented on Apr 14, 2022

    @buyaa-n
    Contributor

    I'm potentially interested in working on this but I'd want to be sure of the expected behavior first.

    Thanks @madelson, the expected behavior is approved, do you still interested in working on this?

  18. madelson commented on Apr 14, 2022

    @madelson
    Contributor

    @buyaa-n sure I'll try to take a look at it this weekend to get a sense of what is involved.

  19. madelson commented on Apr 16, 2022

    @madelson
    Contributor

    @buyaa-n :

    I believe that should depend on if the bool inherit parameter set, as it is false by default, in this case DerivesFromGenericAttribute should not be included.

    I'm a bit confused. Are we talking about the inherit parameter on MemberInfo.GetCustomAttributes(bool)? That parameter is supposed to determine whether to to search this member's inheritance chain to find the attributes; I don't think it affects type-based filtering of the attributes.

    For example:

    typeof(RegexOptions).GetCustomAttributes(typeof(Attribute), inherit: false) // returns the FlagsAttribute
    

    What am I missing?

  20. buyaa-n commented on Apr 18, 2022

    @buyaa-n
    Contributor

    You are right, I have mistaken about the parameter.

    In this case the behavior should be same as how it works for non generic attributes, and Attribute.GetCustomAttributes(,typeof(TestType), typeof(NonGenericAttribute)) returns all derived attribute by default

  21. added a commit that references this issue on Apr 18, 2022
    62e6b59
  22. ghost added
    in-pr
    on Apr 18, 2022
  23. ghost removed
    in-pr
    on Apr 22, 2022
  24. ghost locked as resolved and limited conversation to collaborators on May 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Priority:3api-approvedarea-System.Reflection

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions