Understanding the expressions API C# with Example



Understanding the expressions API C# with Example

We're going to use the expression tree API to create a CalculateSalesTax tree. In plain English, here's a summary 
of the steps it takes to create the tree. 
1. Check if the product is taxable 
2. If it is, multiply the line total by the applicable tax rate and return that amount 
3. Otherwise return 0 
//For reference, we're using the API to build this lambda expression 
orderLine => orderLine.IsTaxable ? orderLine.Total * orderLine.Order.TaxRate : 0; 
//The orderLine parameter we pass in to the method. We specify it's type (OrderLine) and the name of 
the parameter. 
ParameterExpression orderLine = Expression.Parameter(typeof(OrderLine), "orderLine"); 
//Check if the parameter is taxable; First we need to access the is taxable property, then check if 
it's true 
PropertyInfo isTaxableAccessor = typeof(OrderLine).GetProperty("IsTaxable"); 
MemberExpression getIsTaxable = Expression.MakeMemberAccess(orderLine, isTaxableAccessor); 
UnaryExpression isLineTaxable = Expression.IsTrue(getIsTaxable); 
 

//Before creating the if, we need to create the braches 
//If the line is taxable, we'll return the total times the tax rate; get the total and tax rate, 
then multiply 
//Get the total 
PropertyInfo totalAccessor = typeof(OrderLine).GetProperty("Total"); 
MemberExpression getTotal = Expression.MakeMemberAccess(orderLine, totalAccessor); 
//Get the order 
PropertyInfo orderAccessor = typeof(OrderLine).GetProperty("Order"); 
MemberExpression getOrder = Expression.MakeMemberAccess(orderLine, orderAccessor); 
//Get the tax rate - notice that we pass the getOrder expression directly to the member access 
PropertyInfo taxRateAccessor = typeof(Order).GetProperty("TaxRate"); 
MemberExpression getTaxRate = Expression.MakeMemberAccess(getOrder, taxRateAccessor); 
//Multiply the two - notice we pass the two operand expressions directly to multiply 
BinaryExpression multiplyTotalByRate = Expression.Multiply(getTotal, getTaxRate); 
//If the line is not taxable, we'll return a constant value - 0.0 (decimal) 
ConstantExpression zero = Expression.Constant(0M); 
//Create the actual if check and branches 
ConditionalExpression ifTaxableTernary = Expression.Condition(isLineTaxable, 
multiplyTotalByRate, zero); 
//Wrap the whole thing up in a "method" - a LambdaExpression 
Expression> method = Expression.Lambda>(ifTaxableTernary, orderLine); 
 

0 Comment's

Comment Form

Submit Comment