How do I get a value of a reference type in an Expression?

Posted by Jon Kruger on Stack Overflow See other posts from Stack Overflow or by Jon Kruger
Published on 2010-03-23T20:21:54Z Indexed on 2010/03/23 20:23 UTC
Read the original article Hit count: 370

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about lambda-expressions

Related posts about c#