1
//IsCellDataValid Method returns bool
MethodInfo isCellDataValidMethod = this.GetType().GetMethod("IsCellDataValid", BindingFlags.NonPublic | BindingFlags.Instance);

var IsCellDataValidMethodCall = Expression.Call(ruleEngineInstance, isCellDataValidMethod, new ParameterExpression[] { method params});

var cellDataValidConstantExp = ConstantExpression.IsTrue(IsCellDataValidMethodCall);

//GetCellTypeCount returns Int
var isCellDataValidExpression = Expression.IfThen(cellDataValidConstant,
                                                  GetCellTypeCountMethodCall);

Now I want to compare the output of this cellTypeCountExpression to a user entered value and don't want the expression to be compiled to get the result and then build a Constant Expression

I have something like

var resultBinaryExp = ParameterExpression.LessThan(GetCellTypeCountMethodCall, 
                               Expression.Constant(userEnteredValu), typeof(int));
svick
  • 236,525
  • 50
  • 385
  • 514
Vignesh.N
  • 2,618
  • 2
  • 25
  • 33
  • What is `onCellDataValid` supposed to represent? The way you have written it it's equivalent to `var onCellDataValid = IsCellDataValidMethodCall` at best (at worst the `as` produces a `null` value). That's an expression that evaluates to a boolean. How is it related to anything that counts things? – Jon Apr 15 '13 at 12:58
  • you didn't notice the `IfTrue` call ? onCellDataValid will hold `GetCellTypeCountMethodCall` as a `MethodCallExpression` and I guess from my problem I need to use the `ConstantExpression.IsTrue` let me try and post back. – Vignesh.N Apr 15 '13 at 13:01
  • I have edited the code and tested it works without exception, is there anyother way I can do this ? – Vignesh.N Apr 15 '13 at 13:11
  • I did notice the `.IfTrue`. `Expression.IfThen(a, b).IfTrue` is the same thing as simply `a`. So what were you trying do achieve? – Jon Apr 15 '13 at 13:15
  • No... `IfTrue` returns `b` if `a` is executed to `true` – Vignesh.N Apr 15 '13 at 13:21
  • I made a small mistake -- it's not `a`, it's `b` (but that does not change the idea). You are wrong, `IfTrue` does not return `b` if `a` evaluates to `true` -- it cannot do that, because it cannot evaluate `a`. These are expression trees, not runnable code. It returns `b` unconditionally. – Jon Apr 15 '13 at 13:28
  • Proof: `Console.WriteLine(Expression.IfThen(Expression.Constant(false), Expression.Constant(42)).IfTrue)` prints the "42" expression. – Jon Apr 15 '13 at 13:32
  • Why don't you want to compile the expression? I think that's the best way to do this. – svick Apr 15 '13 at 13:53
  • @Jon what I meant was `b` is a placeholder expression that I want executed(when `IsCellDataValidMethodCall` returns `true`) when I finally compile the expression after Building the entire expression tree. – Vignesh.N Apr 15 '13 at 15:29

0 Answers0