I suggest you ...

Allow delegates and generics to be used in attributes

Alter section 17.1.3 of the C# language specification to allow for additional language features to be used in attributes. For example, being able to specify a delegate (anonymous or not) would allow one to look up a member-specific validation method using a static method:

public static bool NameValidator(string name) {
return !string.IsNullOrEmpty(name);
}

[StringValidation(NameValidator)]
public string Name { get; set; }

One better would be to allow anonymous delegates or lambda expressions:

[StringValidation(str => !string.IsNullOrEmpty(str))]
public string Name { get; set; }

Better yet would be to allow for generics in attributes:

[Validation<string>(str => !string.IsNullOrEmpty(str))]
public string Name { get; set; }

213 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    thzincthzinc shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    8 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • thzincthzinc commented  ·   ·  Flag as inappropriate

        Wow! I didn't realize this was up to 87 votes. I, too, have wanted this for some time now. Thanks to all for support in this. Maybe we can get Microsoft to pay attention to this feature request.

      • SirisianSirisian commented  ·   ·  Flag as inappropriate

        I need the ability to use lambdas in attributes. Was looking for this to upvote since I ran into a problem where this is the only elegant solution. This is extremely powerful when you want to do conversion operators in a serialization library where every property requires custom operators. 3 votes.

      • Daniel GrunwaldDaniel Grunwald commented  ·   ·  Flag as inappropriate

        The problem is that such complex attribute values cannot be directly represented in IL. A solution to that could be to compile such an attribute into a *method* that constructs the attribute instance as runtime. Then C# could support arbitrary expressions in attributes just by compiling them into that method.

        Of course, this also requires changes to the metadata format and System.Reflection to support such method-based attributes.

      • Frédéric DecréquyFrédéric Decréquy commented  ·   ·  Flag as inappropriate

        I think we can't go very far only with descriptor parameters and we want. The must is to have the same thing as a DataColumn object for each row value but for all rows, and add some lambda expressions on some event handlers, but By Code !

      Feedback and Knowledge Base