LINQ的Expression.Quote方法的目的是什么? [英] What is the purpose of LINQ's Expression.Quote method?
问题描述
MSDN文档指出:
Expression.Quote
方法创建
UnaryExpression表示
表达式,其值为
,类型为Expression。
Method Creates a UnaryExpression that represents an expression that has a constant value of type Expression.
我已经能够通过使用Expression类手动构建LINQ查询来构建谓词表达式,但从未遇到过Expression.Quote的需求。
I've been able to build predicate expressions for use in LINQ queries by manually constructing them using the Expression class, but have never come across the need for Expression.Quote.
你什么时候和为什么要使用这个?从LINQ表达式我已经看到有这些,他们似乎包装现有的表达式,而不添加任何值。
When and why would you use this? From the LINQ expressions I've seen that have them, they just seem to wrap existing expressions without adding any value.
Quote方法/节点类型的目的是什么?
What is the purpose of the Quote method/node type?
推荐答案
Expression.Quote
指定要对待的lambda作为一个表达式树而不是一个函数。它在其操作数上引发关闭语义。
Expression.Quote
specifies that a lambda is to be treated as an expression tree and not as a function. It induces closure semantics on its operand.
当您使用表达式构造一个
,任何lambda表达式的参数( MethodCallExpression
.Call LambdaExpression
/ Expression< TDelegate>
)必须使用 Expression.Quote
来传递参数。
When you are constructing a MethodCallExpression
using Expression.Call
, any parameters that are lambda expressions (LambdaExpression
/Expression<TDelegate>
) must use Expression.Quote
to wrap the parameter before passing in.
因此,对于<$ c类型的参数$ c> Expression< Func< bool>> ,当您创建一个实例,如:()=> true
,表达式的类型
属性将是 Func< bool>
而表达式的类型调用 GetType
)将是表达式< Func< bool>
So for a parameter of type Expression<Func<bool>>
, when you create an instance such as: () => true
, the expression's Type
property would be Func<bool>
whereas the expression's type (calling GetType
) would be Expression<Func<bool>>
所以要获得一个表达式
,它具有 Type
属性的正确值,可以将lambda表达式传递给 Expression.Quote
并将其作为参数传递给 Expression.Call
。
So to get an Expression
that has the correct value for the Type
property you pass the lambda expression into Expression.Quote
and pass that as the parameter to Expression.Call
.
我通过反光镜看过 Expression.Quote
,而唯一的参数是表达式
,它必须从 LambdaExpression
派生,这在方法中被检查。感兴趣的是,任何人都知道为什么MS不会使参数类型为 LambdaExpression
?
I had a look at Expression.Quote
through reflector and while the sole parameter is of type Expression
, it must derive from LambdaExpression
and this is checked inside the method. Out of interest, anyone know why MS didn't just make the parameter type be LambdaExpression
?
由于StevenH指出out, Expression.Quote
用于实现LINQ查询提供程序。 可查询
中的所有方法都采用lambda表达式,例如 Where
, OrderBy
, GroupBy
等内部构造一个 MethodCallExpression
使用 Expression.Call
并用 Expression.Quote
调用包装lambda表达式参数。
As StevenH pointed out, Expression.Quote
is used in implementing LINQ Query Providers. All the methods on Queryable
that take a lambda expression such as Where
, OrderBy
, GroupBy
, etc internally construct a MethodCallExpression
using Expression.Call
and wrap the lambda expression parameters with Expression.Quote
calls.
有关更详细的说明 Expression.Quote
阅读此答案。
For a more detailed explanation of Expression.Quote
read this answer.
这篇关于LINQ的Expression.Quote方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!