Attributes on generic type arguments #9722
Replies: 1 comment 1 reply
-
Hi @mariusz96, Currently, C# does not allow attributes on generic type parameters or collection elements. This is a language limitation rooted in how attributes and generics are handled at the runtime level. Unlike Java, where type annotations on generics are supported, C# attributes are designed to apply to declarations, not to individual type arguments. Some workarounds exist: 1 - Wrapper types: You can create a custom type that carries the attribute and use it in collections. 2 - Validation frameworks: Instead of attaching attributes directly to the generic type parameter, apply validation or metadata at the property or method level. The feature you suggested would indeed make C# attributes more expressive, and it has been discussed in Roslyn and language design proposals, but it is not yet implemented. If this addresses your question, I’d appreciate it if you could mark it as the accepted answer. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently it is not allowed to attribute generic type arguments, especially collection elements ie:
Allowing it would make C# attributes more expressive, less limited, and on par with Java where this is possible.
Beta Was this translation helpful? Give feedback.
All reactions