views:

40

answers:

1

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

+4  A: 
(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
leppie
(expr.Body as MemberExpression) returns null.
alexn
I see you updated the question. As you are returning object, there will be a Convert expression wrapping the body, will update answer.
leppie
You are my god right now, thanks for this!
alexn
@alexn: Only because I helped another soul out with a similar problem not a week or 2 ago :)
leppie