This project is read-only.

Generic attributes

Topics: C# Language Design
Apr 14, 2014 at 10:38 AM

Generic attributes are supported by CLR, but they cannot be created or consumed with c# - not sure how about other languages. This is not critical feature, but it brings two advantages to the table:
  • few characters less to type. Consider
You win 6 characters per each Type argument plus eventual 2 characters for "()" which you can omit if no more arguments left.
  • with generics, you can use constraints. Now you need to perform validation inside attribute ctor. Which is not hard, but always needs non trivial amount of reflection code. What is more, constraints are validated at compile time, which will quickly notify user in case incorrect type is used. Also, this point will get stronger with each improvement done in the generic constraints area - think of "enum" constraint for example
Upgrade path for existing attributes is super easy and brings no breaking changes. One just needs to define new generic attribute inheriting from existing one and add proper constraints if relevant. Will BCL people do it for existing framework code is another question.

Again - it's not something I'd kill for. But given the fact that metaprogramming gains more and more popularity, all improvements for attributes are worth of consideration.

What do you think?