Retrieving Property name from lambda expression

前端 未结 21 1702
迷失自我
迷失自我 2020-11-21 11:12

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have.

eg.

GetSortingInfo         


        
21条回答
  •  死守一世寂寞
    2020-11-21 11:59

    This might be optimal

    public static string GetPropertyName(Expression> expr)
    {
        var memberAccess = expr.Body as MemberExpression;
        var propertyInfo = memberAccess?.Member as PropertyInfo;
        var propertyName = propertyInfo?.Name;
    
        return propertyName;
    }
    

提交回复
热议问题