Posted By

jmcd on 06/24/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


Narrowing an expression to use a delegate with fewer args


 / Published in: C#
 

  1. var valueFromSomewhere = "BAR";
  2.  
  3. Expression<Func<Controller, string, object>> expressionWithArg = (c, s) => c.Action("foo", s);
  4.  
  5. var methodCallExpression = (MethodCallExpression)expressionWithArg.Body;
  6.  
  7. var expressionWithoutArg = Expression.Lambda<Func<Controller, object>>(
  8. Expression.Call(methodCallExpression.Object, methodCallExpression.Method, methodCallExpression.Arguments[0], Expression.Constant(valueFromSomewhere)),
  9. expressionWithArg.Parameters[0]
  10. );

Report this snippet  

You need to login to post a comment.