获取表达式属性名称

/// <summary>
/// 获取表达式属性名称
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="expr"></param>
/// <returns></returns>
private string GetExpressionMemberName<TModel, TValue>(System.Linq.Expressions.Expression<Func<TModel, TValue>> expr)
{
var unary = expr.Body as UnaryExpression;
MemberExpression member;
if (unary != null)
{
member = unary.Operand as MemberExpression;
}
else
{
member = expr.Body as MemberExpression;
}

if (member == null)
{
throw new Exception("表达式解析异常");
}

var itemName = member.Member.Name;  //属性名
return itemName;
}

posted @ 2017-04-24 15:35  玻璃鱼儿  阅读(185)  评论(0编辑  收藏  举报