public static string GetPropertyInfo<T, TR>(Expression<Func<T, TR>> select)
{
var body = select.Body;
if (body.NodeType == ExpressionType.Convert)
{
var o = (body as UnaryExpression).Operand;
return (o as MemberExpression).Member.Name;
}
else if (body.NodeType == ExpressionType.MemberAccess)
{
return (body as MemberExpression).Member.Name;
}
return null;
}
我希望得到类似这样的结果,
string ID=GetPropertyInfo<People>(t=>t.ID); 返回字符串"ID"
已经解决了