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
[API Proposal]: Get info about generic attributes #64169
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
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.
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.
@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?
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.
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.
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
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
Activity
CyrusNajmabadi commentedon Jan 24, 2022
I'm not certain why this is needed. Why not just do
.GetType().GenericTypeArguments?buyaa-n commentedon Jan 28, 2022
For adding a new property i agree with @CyrusNajmabadi, doesn't seem we need to add property for
TypeArgsMakes 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-suggestionlabelmadelson commentedon Feb 12, 2022
Just to clarify, is the proposal that calling any
GetCustomAttributes()method with an open generic type deriving fromAttributewill return all attributes which are or derive from generic instantiations of that type? So if we had: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.
steveharter commentedon Feb 15, 2022
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.
steveharter commentedon Feb 15, 2022
@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?
buyaa-n commentedon Feb 16, 2022
I believe that should depend on if the
bool inheritparameter set, as it is false by default, in this caseDerivesFromGenericAttributeshould not be included.1 remaining item
terrajobst commentedon Apr 14, 2022
Yes, reflection should support retrieving attribute by open generic types. And yes, using a base type should also work, assuming
inheritis set totrue.buyaa-n commentedon Apr 14, 2022
Thanks @madelson, the expected behavior is approved, do you still interested in working on this?
madelson commentedon Apr 14, 2022
@buyaa-n sure I'll try to take a look at it this weekend to get a sense of what is involved.
madelson commentedon Apr 16, 2022
@buyaa-n :
I'm a bit confused. Are we talking about the
inheritparameter onMemberInfo.GetCustomAttributes(bool)? That parameter is supposed to determine whether toto search this member's inheritance chain to find the attributes; I don't think it affects type-based filtering of the attributes.For example:
What am I missing?
buyaa-n commentedon Apr 18, 2022
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 defaultAllow custom attribute filtering with an open generic type.