Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get expression parameter name

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

Map(x => x.Name)

From this, i need "Name".

How do I do this?

I can get x by doing this:

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

But im not able to get "Name". Note that I dont have any instance of T i can invoke on. Thanks

like image 490
alexn Avatar asked Feb 26 '23 06:02

alexn


1 Answers

(expr.Body as MemberExpression).Member.Name

As 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
like image 70
leppie Avatar answered Mar 06 '23 02:03

leppie