C# Method Attribute cannot contain a Lambda Expression?
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 deleg开发者_JAVA技巧ate 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()
{
}
}
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
精彩评论