编前pression树木误会? [英] Compiled Expression Trees misunderstanding?
问题描述
我有这样的EX pression:
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
我想请教一下 .Compile()
这是什么编制?什么是VS后执行的第一个执行之间的区别...?
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 ?
推荐答案
在正在建设的前pression树在运行时有没有发出code。这是一种方式重新present .NET code运行时。
When you are building the expression tree at runtime there's no code emitted. It's a way to represent .NET code at runtime.
在你呼吁前pression树实际IL code被发射到该EX pression树转换成 .Compile
方法委托( Func键&LT;字符串,布尔&GT;
你的情况),你可以调用在运行时。所以,这个前pression树重新presents只能在您需要执行的code编译。
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.
调用编译是一个昂贵的操作。基本上你应该有一次他称呼它,然后缓存所产生的委托,你可以用它来调用code很多次了。
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.
这篇关于编前pression树木误会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!