Well, just to summarize the answers already given, and based on them, I've created two helper methods to help with this task when needed, depending on the lambda format used:
public String ObterNome<TIn>(Expression<Func<TIn, object>> expression)
{
return ObterNome(expression as Expression);
}
public String ObterNome<TOut>(Expression<Func<TOut>> expression)
{
return ObterNome(expression as Expression);
}
private String ObterNome(Expression expression)
{
var lambda = expression as LambdaExpression;
var member = lambda.Body.NodeType == ExpressionType.Convert
? ((UnaryExpression)lambda.Body).Operand as MemberExpression
: lambda.Body as MemberExpression;
return member.Member.Name;
}
There is a method to get the name, if you use a lambda with closure, which points to the property, and another one in case there is no closure, but need to indicate the generic parameter type.