C# Expression Tree Simple Arithmetic
Posted
by
Richard Adnams
on Stack Overflow
See other posts from Stack Overflow
or by Richard Adnams
Published on 2010-12-22T10:49:49Z
Indexed on
2010/12/22
10:54 UTC
Read the original article
Hit count: 342
Hello,
I've been trying to figure out how to achieve some simple maths using the Expression class.
What I'm trying to do is this
(1 + 10 * 15)
When I try to do this via Expression.Add and Expression.Constant but the result I get is this
((1 + 10) * 15)
Which is not right as it evaluates the 1 + 10 first instead of 10 * 15.
Is there a way to combine Expression.Add/Multiply etc.. without it creating the brackets? I assume there is but I just can't find where or how!
The test code I have is this
var v1 = Expression.Constant(1, typeof(int));
var v2 = Expression.Constant(10, typeof(int));
var v3 = Expression.Constant(15, typeof(int));
var a1 = Expression.Add(v1, v2);
var m2 = Expression.Multiply(a1, v3);
Thanks for your time,
Richard.
© Stack Overflow or respective owner