public static void Map<T>(Expression<Func<T, object >> expression)
{
    var memberExpression = (expression.Body as UnaryExpression).Operand as MemberExpression;
    var name = memberExpression.Member.Name;
}


用法:

Map<Article>(x => x.Name)


名称是字符串类型。

如何获得类型?

最佳答案

var returnType = memberExpression.Member is MethodInfo
    ? ((MethodInfo)memberExpression.Member).ReturnType
    : ((PropertyInfo)memberExpression.Member).PropertyType;


我假设您想要返回类型。如果要使用定义方法的类型,请使用memberExpression.Member.DeclaringType

09-18 09:32