Extension method on lambda expression
c#, extension-methods, lambda
Solution
You can't do that, because a lambda expression has no type by itself; its type is determined by the context (e.g. if you assign it to a delegate variable or pass it as an argument to a method).
Since `((Thing t) => t.Property)` doesn't have a type, you can't call an extension method on it, because the compiler doesn't know which extension methods are valid candidates.
You can, however, declare a variable and call the extension method on it:
Func<Thing, OtherThing> func = t => t.Property;
string name = func.GetName();
Problem
I have a helper method which gets the name of a property defined by a lambda which works as below: ``` ExpressionUtil.GetName((Thing t) => t.Property); // returns "Property" ``` I would like to turn this into an extension method so the syntax would be of the form: ``` ((Thing t) => t.Property).GetName(); // wont compile : operator '.' cannot be applies to operand of type 'lambda expression' ``` However I cant seem to do this as `((Thing t) => t.Property)` is a lambda (not an expression or Func yet). Is there any way to write an extension method which applies directly to a lambda? If not why is this a bad thing to do?