0

When you are building the expression tree at runtime there's no code emitted. It's a way to represent .NET code at runtime...

Ok...

Now lets say I have this code :

    ParameterExpression p = Expression.Parameter (typeof (string), "s");
    MemberExpression stringLength = Expression.Property (p, "Length");
    ConstantExpression five = Expression.Constant (5);
    BinaryExpression comparison = Expression.LessThan (stringLength, five);
    Expression<Func<string, bool>> lambda= Expression.Lambda<Func<string, bool>> (comparison, p);

    Func<string, bool> runnable = lambda.Compile();

This code Wont be in IL ? of course it will be ! ( maybe the last line wont emit code until compile ...but the first lines I think will emit code !)

So what am i saving here ?

Ok so the first 5 lines did emit code and the last one didn't... big deal.

What am i missing ? Can you please let me see the whole picture ?

6
  • When you call compile, the expression tree is converted to IL (using DynamicMethod). Commented Feb 27, 2012 at 9:02
  • @leppie and the first 5 lines wont be in IL ? Commented Feb 27, 2012 at 9:05
  • Well, that is normal code that gets compiled initially. Expression have no concept of IL, it is an abstraction, that can be emitted to IL, but there is no requirement to do so (you can for example emit your own x86 ASM or GPU code etc). Commented Feb 27, 2012 at 9:07
  • Where did you get that statement? Commented Feb 27, 2012 at 9:23
  • 1
    I think he meant it as "IL code of resulting delegate" not "IL code, that creates the expression". Commented Feb 27, 2012 at 9:31

2 Answers 2

2

With an Expression Tree, you build a description of some code instead of the code itself. Expression Trees should not be used in the context of writing regular code that 'shouldn't be compiled at compile time'. They should be used in more dynamic scenarios.

The expression tree you show will compile to: s.Length < 5 and you invoke the runnable with bool isStringSmallerThan5 = runnable("MyString").

The whole idea of Expression Trees is that they describe some code and can be compiled at runtime. This means that you can do the following:

BinaryExpression comparison = null;
if (lessThen)
{
    comparison = Expression.LessThan(stringLength, five);
}
else
{
    comparison = Expression.GreaterThan(stringLength, five);
}

Now you can change the behavior of your code at runtime!

The biggest use of Expression Trees is that they can be interpreted by a provider. For example Linq To Entities uses Expression Trees and compiles them to SQL code that can be run against the database. LinqToXml is another example of what you can do with Expression Trees.

This a nice blog post to get you started.

Sign up to request clarification or add additional context in comments.

4 Comments

I can see HOw Im gonna do 9000 If's in my code If i want it to act dynamically....
What is it that you want to achieve? Expression Trees are mostly used in things like Linq. Creating real dynamic code is not the main scenario. And if you have '9000' different scenarios, how do you want to support them?
I see.... Can you please give a tiny little example how linq uses that in a most simple query ?
I've added a link to an MSDN blog post.
0

Expression trees are useful when you receive them in a method since they enable you to make more complex use of the expression content. If you receive a predicate in a method, you can run it against a target and check the result. If you receive the expression tree representing an expression tree, you can parse it and do something useful with it. An example is LINQ which utilizes this many places, but among one in the "Where"-methods. Catching the expression tree rather than the IL makes it relatively straight forward to translate into SQL rather than just do a full 'Select' and run predicates against the materialized result.

2 Comments

thanks but I asked my question as the "Code emitting" point of view... what am i saving here ? and If i get instead of a predicate - an Expression tree - I will still have to "compile" it... so what did I earn ? I cant catch the point ( and I know there is one :) ).
IMHO the point is that you have the more "friendly" intermeddiate representation. But you definately will have to compile it at some point. Lets see if someone cleverer than me have a better explanation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.