ReplaceVisitor.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. namespace ExpressionKit.Unwrap
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Web;
  8. /// <summary>
  9. /// An expression visitor that acts as a simple find
  10. /// and replace.
  11. /// </summary>
  12. internal class ReplaceVisitor : ExpressionVisitor
  13. {
  14. /// <summary>
  15. /// The expression to find.
  16. /// </summary>
  17. private readonly Expression Source;
  18. /// <summary>
  19. /// The replacement expression.
  20. /// </summary>
  21. private readonly Expression Replacement;
  22. /// <summary>
  23. /// Create a new replacement visitor.
  24. /// </summary>
  25. /// <param name="source">
  26. /// The expression to find.
  27. /// </param>
  28. /// <param name="replacement">
  29. /// The replacement expression.
  30. /// </param>
  31. public ReplaceVisitor(Expression source, Expression replacement)
  32. {
  33. this.Source = source;
  34. this.Replacement = replacement;
  35. }
  36. public override Expression Visit(Expression node)
  37. {
  38. if (this.Source == node) return this.Replacement;
  39. return base.Visit(node);
  40. }
  41. }
  42. }