Update: This does work, I was being stupid :(
i have the following extension method
public static string ExtMethod(this object self, object myparameter);
at runtime this is called in any number of ways ways, i think these are all possibilities:
Expression<Func<T, string>> expr = x => x.property.ExtMethod(5);
Expression<Func<T, string>> expr = x => x.property.ExtMethod(new object());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.someMethod());
Expression<Func<T, string>> expr = x => x.property.ExtMethod(x.OtherProperty);
what i need to do is evaluate the "myparameter
", given "expr
" and a "T
"
because of the two cases where x
is used in myparameter
, i thought i needed to create a delegate of the form:
Expression<Func<T, object>> expr = x => [myparameter expression here]
i thought this would work:
var extMethodExpr = expr.Body as MethodCallExpression;
var myparameterExpr = extMethodExpr.Arguments[1];
var myparam = Expression.Lambda(myparameterExpr, expr.Parameters).Compile().Invoke(someT)
but for the expressions that do not involve x
, i get TargetParameterCountException
:(
in these cases, if i do:
var myparam = Expression.Lambda(myparameterExpr).Compile().Invoke(someT)
it works fine.
How do I solve this?
thanks