Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Method Attribute cannot contain a Lambda Expression?

Tags:

IntelliSense is telling me "Expression cannot contain anonymous methods or lambda expressions." Really? I was not aware of this imposed limitation. Is this correct? I guess I'm looking for a sanity check here...

 public delegate bool Bar(string s);  [AttributeUsage(AttributeTargets.All)] public class Foo : Attribute {     public readonly Bar bar;      public Foo(Bar bar)     {         this.bar = bar;     } }  public class Usage {     [Foo(b => b == "Hello World!")]        // IntelliSense Complains here     public Usage()     {     } } 
like image 574
Didaxis Avatar asked Dec 10 '10 16:12

Didaxis


1 Answers

Yes this is correct. Attribute values are limited to constants of the following types

  • Simple types (bool, byte, char, short, int, long, float, and double)
  • string
  • System.Type
  • enums
  • object (The argument to an attribute parameter of type object must be a constant value of one of the above types.)
  • One-dimensional arrays of any of the above types

Reference: http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx

like image 86
JaredPar Avatar answered Oct 09 '22 03:10

JaredPar