tags:

views:

99

answers:

4

I have this method:

public void DoSomething<T>(Expression<Func<T, object>> method)
{
}

If this method is called like this:

DoSomething(c => c.SomeMethod(new TestObject()));

... how do I get the value of the parameter that was passed into SomeMethod()?

If the parameter is a value type, this works:

var methodCall = (MethodCallExpression)method.Body;
var parameterValue = ((ConstantExpression)methodCall.Arguments[0]).Value;

However, when I pass in a reference type, methodCall.Arguments[0] is a MemberExpression, and I can't seem to figure out how to write code to get the value out of it.

+1  A: 

This isn't really a matter of value type or reference type - it's a matter of a constant expression or not-constant expression. I'm sure your existing code would fail if you called

DoSomething(c => c.SomeMethod(DateTime.Now));

as well.

Basically, the argument to the method is just an expression. It's not a value. You could potentially compile that expression and then execute it to get the value at that point in time, but it's important to understand that an expression isn't a value in itself. In the case of a constant expression it's easy, but taking the DateTime.Now example, by definition the evaluated value of the expression changes over time :)

What are you trying to do with the argument? What's the bigger picture here?

Jon Skeet
@Downvoters: Any chance you could actually leave some indication of what you dislike about my answer? 2 downvotes with no comments is a bit strange...
Jon Skeet
Nothing personal maybe, I'm guessing that @akasha's answer got closer to giving OP what he wanted. Probably the downvoters already used an upvote for @akahsa and thought it should sit higher than your answer. I'll give you both a +1 for good karma.
Michael Meadows
A: 

Firstly, what Jon said.

There are no values to get ahold of, it's all just expressions. What might be of interest is the NewExpression, which has a Constructor property; this property contains the reflected constructor that would be called if you compiled expression and ran the resulting delegate. You could manually invoke that constructor and get an instance of what the user was intending on instantiating.

James Gregory
+3  A: 

You will have to evaluate member expression manually, MemberExpression contains "Expression" which is the container object of Member specified, to do this you can crete Lamda of your arguement and compile and execute it.

LamdaExpression l = Expression.Lamda(methodCall.Arguments[0]);
var c = l.Compile();
var v = l.Invoke();

So whatever you pass, you will get it in "v" variable.

Akash Kava
A: 

Here is the answer (inspired by Akash's answer):

LambdaExpression lambda = Expression.Lambda(methodCall.Arguments[0]);
var compiledExpression = lambda.Compile();
return compiledExpression.DynamicInvoke();
Jon Kruger
Just be aware that that's the value of the expression *at that point in time*. You should also consider passing in the original expression's parameters, in case they're used in the subexpression.
Jon Skeet
That's fine, this is for a test assertion helper method so all I need to know is that value at that point in time.
Jon Kruger