1

I am stuck in expression tree land. I have an expression that calls an .Any() method which returns a Boolean value. Based on the value of the .Any() I want to set a local variable to some value. I then want to set that same local variable in other conditional calls..

var anyExpression = ...;
Expression.Block(new [] {myLocalVarClass, Param}, 
Expression.Assign(mylocalVarClass, ..), 
Expression.Assign(Param, ...) 
Expression.Condition(anyExpression, 
Expression.Block(...set myLocalVarClass because any is true,  Expression.Constant(true)), 
Expression.Block(...set myLocalVarClass because any is false, Expression.Constant(false)));

in the ifTrue part of the condition i set myLocalVarClass members and as the last expression of the ifTrue block I set it to Expression.Constant(true))

I would like to take the block containing the condition, AND_ALSO it to another block of the same structure. The blocks should use the same myLocalVarClass (to set various class members) as well as the parameter (which is a type of class) passed in to the compiled lambda. The final return type isn't bool it should be myLocalVarClass.

After I AND the blocks together into finalJoinedExpression I try this

var finalBlk = Expression.Block(new[] { myLocalVarClass, Param}, Expression.Assign(myLocalVarClass, ...),
Expression.Assign(Param, ...),
finalJoinedExpression, returnExpression, returnLabel);

// returnExpression, returnLabel correctly point to myLocalVarClass to return.

this.doesntWork =
              Expression.Lambda<Func<T, myLocalVarClass>>(finalBlk, param).Compile();

this.doesntWork(paramClassObj);

throws a error:

object reference not set to an instance of an object

Any help guys would be much appreciated.

crthompson
  • 14,783
  • 6
  • 51
  • 74
  • I see no reason why this question is not an ordinary NRE question. Furthermore, you have not posted details about the error. – usr Jul 28 '14 at 23:47

0 Answers0