Narrowing an expression to use a delegate with fewer args


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.