2

I got a linq expression,

internal T Execute<T>(System.Linq.Expressions.Expression<Func<int, string, T>> expr)
{
    var paramInt = ??;
    var paramString = ??;
}

I call this method using this:

Expression<Func<int, string, Guid>> myExpression2 = (a,b) => Callmethod(a, b, 5);

Execute<Guid>(myExpression2);

How can i get the parameters from the expression in my execute method ?

-- To clarify --

I want to get the values from the parameters so that i do some calculations with them.

Patrick
  • 5,442
  • 9
  • 53
  • 104

1 Answers1

3

Well, you can get the parameters using the Parameters property.

var parameters = expr.Parameters;

However, each of those will be a ParameterExpression. That will give you the parameter name and the type, but it's not clear what you want to do with them.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I want to get the values from the parameters so that i do some calculations with them. – Patrick Aug 23 '11 at 09:20
  • @Patrick: The parameters don't *have* values - they're *parameters*. It's like asking what the value of a method parameter is - it only has a value when it's invoked. – Jon Skeet Aug 23 '11 at 09:27
  • @Jon, I think he want to extract the values passed within the expression. – Filip Ekberg Aug 23 '11 at 09:29
  • @Filip: But which values in this case? The only value we've got is 5, which is the argument for the method call *within* the expression tree... – Jon Skeet Aug 23 '11 at 09:31
  • @Jon, Right, but when they are invoked they must have a value, no? So that value is the one you want to extract. The 5 for instance, would that be possible to extract? – Filip Ekberg Aug 23 '11 at 09:36
  • @JonSkeet could you please shed some light into this one: http://stackoverflow.com/q/17796800/114029 – Leniel Maccaferri Jul 22 '13 at 21:04