ExpressionValidator.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Reflection;
  8. using System.Collections.Specialized;
  9. using System.Linq.Expressions;
  10. namespace System.Linq.jvm
  11. {
  12. internal class ExpressionValidator : ExpressionVisitor
  13. {
  14. LambdaExpression _exp;
  15. internal ExpressionValidator(LambdaExpression exp)
  16. {
  17. _exp = exp;
  18. }
  19. protected override void Visit(Expression expression)
  20. {
  21. if (expression == null)
  22. {
  23. return;
  24. }
  25. if (expression.NodeType == ExpressionType.Power)
  26. {
  27. VisitBinary((BinaryExpression)expression);
  28. }
  29. else
  30. {
  31. base.Visit(expression);
  32. }
  33. }
  34. protected override void VisitParameter(ParameterExpression parameter)
  35. {
  36. foreach (ParameterExpression pe in _exp.Parameters)
  37. {
  38. if (pe.Name.Equals(parameter.Name) &&
  39. !Object.ReferenceEquals(parameter, pe))
  40. {
  41. throw new InvalidOperationException("Lambda Parameter not in scope");
  42. }
  43. }
  44. }
  45. internal void Validate()
  46. {
  47. Visit(_exp);
  48. }
  49. }
  50. }