fork download
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Expression<Func<float,float,float>> f1 = (x, y) => x + y;
  10. Expression<Func<float,float>> f2 = x => x * x;
  11. var pX = f2.Parameters[0];
  12. var pY = f1.Parameters[1];
  13. var replacerF2 = new ReplaceParameter(pX, pY);
  14. var replacerF1 = new ReplaceParameter(pY, replacerF2.Visit(f2.Body));
  15. var modifiedF1 = Expression.Lambda(
  16. replacerF1.Visit(f1.Body)
  17. , f1.Parameters
  18. );
  19. Console.WriteLine(modifiedF1);
  20. }
  21.  
  22. class ReplaceParameter : ExpressionVisitor {
  23. private readonly Expression replacement;
  24. private readonly ParameterExpression parameter;
  25. public ReplaceParameter(
  26. ParameterExpression parameter
  27. , Expression replacement
  28. ) {
  29. this.replacement = replacement;
  30. this.parameter = parameter;
  31. }
  32. protected override Expression VisitParameter(ParameterExpression node) {
  33. return node == parameter ? replacement : node;
  34. }
  35. }
  36. }
Success #stdin #stdout 0.02s 132800KB
stdin
Standard input is empty
stdout
(x, y) => (x + (y * y))