LambdaExpression 类

定义

介绍 lambda 表达式。Describes a lambda expression. 它捕获一个类似于 .NET 方法主体的代码块。This captures a block of code that is similar to a .NET method body.

public ref class LambdaExpression abstract : System::Linq::Expressions::Expression
public abstract class LambdaExpression : System.Linq.Expressions.Expression
type LambdaExpression = class
    inherit Expression
Public MustInherit Class LambdaExpression
Inherits Expression
继承
LambdaExpression
派生

示例

下面的示例演示如何创建一个表达式,该表达式表示通过使用 Lambda 方法将1添加到传递的自变量的 lambda 表达式。The following example demonstrates how to create an expression that represents a lambda expression that adds 1 to the passed argument by using the Lambda method.

// Add the following directive to your file:
// using System.Linq.Expressions;  

// A parameter for the lambda expression.
ParameterExpression paramExpr = Expression.Parameter(typeof(int), "arg");

// This expression represents a lambda expression
// that adds 1 to the parameter value.
LambdaExpression lambdaExpr = Expression.Lambda(
    Expression.Add(
        paramExpr,
        Expression.Constant(1)
    ),
    new List<ParameterExpression>() { paramExpr }
);

// Print out the expression.
Console.WriteLine(lambdaExpr);

// Compile and run the lamda expression.
// The value of the parameter is 1.
Console.WriteLine(lambdaExpr.Compile().DynamicInvoke(1));

// This code example produces the following output:
//
// arg => (arg +1)
// 2
' Add the following directive to your file:
' Imports System.Linq.Expressions  

' A parameter for the lambda expression.
Dim paramExpr As ParameterExpression = Expression.Parameter(GetType(Integer), "arg")

' This expression represents a lambda expression
' that adds 1 to the parameter value.
Dim lambdaExpr As LambdaExpression = Expression.Lambda(
        Expression.Add(
            paramExpr,
            Expression.Constant(1)
        ),
        New List(Of ParameterExpression)() From {paramExpr}
    )

' Print out the expression.
Console.WriteLine(lambdaExpr)

' Compile and run the lamda expression.
' The value of the parameter is 1.
Console.WriteLine(lambdaExpr.Compile().DynamicInvoke(1))

' This code example produces the following output:
'
' arg => (arg +1)
' 2

注解

LambdaExpression 类型表示表达式树形式的 lambda 表达式。The LambdaExpression type represents a lambda expression in the form of an expression tree. Expression<TDelegate> 类型从 LambdaExpression 派生,并更明确地捕获 lambda 表达式的类型,也可用于表示 lambda 表达式。The Expression<TDelegate> type, which derives from LambdaExpression and captures the type of the lambda expression more explicitly, can also be used to represent a lambda expression. 在运行时,表示 lambda 表达式的表达式树节点始终为类型 Expression<TDelegate>At runtime, an expression tree node that represents a lambda expression is always of type Expression<TDelegate>.

LambdaExpressionNodeType 属性的值 LambdaThe value of the NodeType property of a LambdaExpression is Lambda.

使用 Lambda 工厂方法创建 LambdaExpression 对象。Use the Lambda factory methods to create a LambdaExpression object.

属性

Body

获取 lambda 表达式的主体。Gets the body of the lambda expression.

CanReduce

指示可将节点简化为更简单的节点。Indicates that the node can be reduced to a simpler node. 如果返回 true,则可以调用 Reduce() 以生成简化形式。If this returns true, Reduce() can be called to produce the reduced form.

(继承自 Expression)
Name

获取 lambda 表达式的名称。Gets the name of the lambda expression.

NodeType

返回此 Expression 的节点类型。Returns the node type of this Expression.

Parameters

获取 lambda 表达式的参数。Gets the parameters of the lambda expression.

ReturnType

获取 lambda 表达式的返回类型。Gets the return type of the lambda expression.

TailCall

获取一个值,该值指示是否将通过尾调用优化来编译 lambda 表达式。Gets the value that indicates if the lambda expression will be compiled with the tail call optimization.

Type

获取此 Expression 表示的表达式的静态类型。Gets the static type of the expression that this Expression represents.

方法

Accept(ExpressionVisitor)

调度到此节点类型的特定 Visit 方法。Dispatches to the specific visit method for this node type. 例如,MethodCallExpression 调用 VisitMethodCall(MethodCallExpression)For example, MethodCallExpression calls the VisitMethodCall(MethodCallExpression).

(继承自 Expression)
Compile()

生成表示 lambda 表达式的委托。Produces a delegate that represents the lambda expression.

Compile(Boolean)

生成表示 Lambda 表达式的已解释或已编译委托。Produces an interpreted or compiled delegate that represents the lambda expression.

Compile(DebugInfoGenerator)

生成表示 lambda 表达式的委托。Produces a delegate that represents the lambda expression.

CompileToMethod(MethodBuilder)

将 lambda 编译到方法定义中。Compiles the lambda into a method definition.

CompileToMethod(MethodBuilder, DebugInfoGenerator)

将 lambda 编译到方法定义和自定义调试信息中。Compiles the lambda into a method definition and custom debug information.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Reduce()

将此节点简化为更简单的表达式。Reduces this node to a simpler expression. 如果 CanReduce 返回 true,则它应返回有效的表达式。If CanReduce returns true, this should return a valid expression. 此方法可以返回本身必须简化的另一个节点。This method can return another node which itself must be reduced.

(继承自 Expression)
ReduceAndCheck()

将此节点简化为更简单的表达式。Reduces this node to a simpler expression. 如果 CanReduce 返回 true,则它应返回有效的表达式。If CanReduce returns true, this should return a valid expression. 此方法可以返回本身必须简化的另一个节点。This method can return another node which itself must be reduced.

(继承自 Expression)
ReduceExtensions()

将表达式简化为已知节点类型(即非 Extension 节点)或仅在此类型为已知类型时返回表达式。Reduces the expression to a known node type (that is not an Extension node) or just returns the expression if it is already a known type.

(继承自 Expression)
ToString()

返回 Expression 的的文本化表示形式。Returns a textual representation of the Expression.

(继承自 Expression)
VisitChildren(ExpressionVisitor)

简化节点,然后对简化的表达式调用访问者委托。Reduces the node and then calls the visitor delegate on the reduced expression. 该方法在节点不可简化时引发异常。The method throws an exception if the node is not reducible.

(继承自 Expression)

适用于