Update: it works, I was stupid: (
I have the following extension method
public static string ExtMethod(this object self, object myparameter);
at runtime, this is called any number of 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 " myparameter ", given " expr " and " T "
due to two cases where x used in myparameter , I thought I needed to create a form delegate:
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 expressions that do not include x , I get TargetParameterCountException : (
in these cases, if I:
var myparam = Expression.Lambda(myparameterExpr).Compile().Invoke(someT)
It works great.
How to solve this?
thanks