I have this expression :
Expression<Func<string, bool>> f = s => s.Length < 5;
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);
//lets : test
Func<string, bool> runnable = lambda.Compile();
Console.WriteLine (runnable ("kangaroo")); // False
Console.WriteLine (runnable ("dog")); //True
I want to ask about the .Compile()
What does it compile ? And what is the difference between the first execution vs later executions...?
Compile should be something that happens once and not happens again later ....
What / How does it help me ?
Expression tree compilation for example has a built in interpreter so it still "works". The problem is that its often slower than just using reflection, especially since Native AOT has "faster reflection".
When you want to have a richer interaction, you need to use Expression Trees. Expression Trees represent code as a structure that you can examine, modify, or execute. These tools give you the power to manipulate code during run time. You can write code that examines running algorithms, or injects new capabilities.
Expression trees are often used to generate code dynamically at runtime. Lets say that you have to create lots of unknown type insatnces. You could create them using reflection and you'll suffer from poor performance, or you can create an expression tree and compile it to a method.
You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. For more information about expression trees in LINQ, see How to use expression trees to build dynamic queries (C#).
When you are building the expression tree at runtime there's no code emitted. It's a way to represent .NET code at runtime.
Once you call the .Compile
method on the expression tree the actual IL code is emitted to convert this expression tree into a delegate (Func<string, bool>
in your case) that you could invoke at runtime. So the code that this expression tree represents can be executed only after you compile it.
Calling Compile is an expensive operation. Basically you should be calling it once and then caching the resulting delegate that you could use to invoke the code many times.
The Expression<Func<string,bool>>
is only a representation of an expression, it cannot be executed. Calling Compile()
gives you a compiled delegate, a piece of code that you can call. Essentially, your program composes a small code snippet at runtime, and then call it as if it were processed by the compiler. This is what the last two lines of your code do: as you can see, the compiled snippet can analyze the length of the string that you pass in - when the length is less than five, you get a True
back; when it's five or more, you get a False
.
What happens on first execution of the compiled snippet is platform-dependent, and should not be detectable by programmers using the .NET platform.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With