Get expression parameter name

I need to get the name of an expression parameter. What I want to do is similar to what FluentNhibernate does with column mapping:

Map (x => x.Name)

From this I need a "Name".

How to do it?

I can get xby doing the following:

Expression<Func<User, object>> exp = x => x.Id;
exp.Parameters[0].Name;

But I could not get the "Name". Note that I do not have an instance of T that can be called. Thanks

+3
source share
1 answer
(expr.Body as MemberExpression).Member.Name

When the expression returns object, the body will be wrapped in a Convert expression.

The following should work.

((expr.Body as UnaryExpression).Operand as MemberExpression).Member.Name
+7
source

Source: https://habr.com/ru/post/1763980/


All Articles