Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda in a attribute parameter

Why can't we add a lambda as attribute parameter? For example I would like to do:

[MyAttribut(Transform= {object => object.ToSomethingElse()})]

Should I understand that lambda in C# are only closure?

EDIT: By looking at all answer let me precise one things that most people seems to not know: Lambda are literal whish is a compile time structure unless it is a closure.

like image 873
mathk Avatar asked Feb 20 '23 05:02

mathk


1 Answers

Parameters to attributes need to be compile time constants.

From MSDN - Attribute Parameter Types:

Values passed to attributes must be known to the compiler at compile time.

They are also restricted to these types:

  • bool
  • char, unsigned char
  • short, unsigned short
  • int, unsigned int
  • long, unsigned long
  • __int64, unsigned __int64
  • float, double
  • wchar_t
  • char* or wchar_t* or System::String*
  • System::Type ^
  • System::Object ^
  • enum
like image 197
Oded Avatar answered Mar 03 '23 12:03

Oded