Well, just to summarize the answers already given, and based on them, I created two auxiliary methods to help in this task when necessary, 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 obtain the name, if a lambda with closure is used, which points to the property, and another to the case where there is no closure, but it is necessary to indicate the type of the generic parameter.
That’s exactly what I wanted. Thanks!
– Miguel Angelo