BlockExpression 类

定义

表示一个块,其中包含可以定义变量的表达式序列。

public ref class BlockExpression : System::Linq::Expressions::Expression
public class BlockExpression : System.Linq.Expressions.Expression
type BlockExpression = class
    inherit Expression
Public Class BlockExpression
Inherits Expression
继承
BlockExpression

示例

下面的代码示例演示如何创建块表达式。 块表达式由两 MethodCallExpression 个对象和一个 ConstantExpression 对象组成。

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

// The block expression allows for executing several expressions sequentually.
// When the block expression is executed,
// it returns the value of the last expression in the sequence.
BlockExpression blockExpr = Expression.Block(
    Expression.Call(
        null,
        typeof(Console).GetMethod("Write", new Type[] { typeof(String) }),
        Expression.Constant("Hello ")
       ),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("World!")
        ),
    Expression.Constant(42)
);

Console.WriteLine("The result of executing the expression tree:");
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
var result = Expression.Lambda<Func<int>>(blockExpr).Compile()();

// Print out the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:");
foreach (var expr in blockExpr.Expressions)
    Console.WriteLine(expr.ToString());

// Print out the result of the tree execution.
Console.WriteLine("The return value of the block expression:");
Console.WriteLine(result);

// This code example produces the following output:
//
// The result of executing the expression tree:
// Hello World!

// The expressions from the block expression:
// Write("Hello ")
// WriteLine("World!")
// 42

// The return value of the block expression:
// 42
' Add the following directive to your file:
' Imports System.Linq.Expressions

' The block expression enables you to execute several expressions sequentually.
' When the block expression is executed,
' it returns the value of the last expression in the sequence.
Dim blockExpr As BlockExpression = Expression.Block(
    Expression.Call(
        Nothing,
        GetType(Console).GetMethod("Write", New Type() {GetType(String)}),
        Expression.Constant("Hello ")
       ),
    Expression.Call(
        Nothing,
        GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
        Expression.Constant("World!")
        ),
    Expression.Constant(42)
)

Console.WriteLine("The result of executing the expression tree:")
' The following statement first creates an expression tree,
' then compiles it, and then executes it.           
Dim result = Expression.Lambda(Of Func(Of Integer))(blockExpr).Compile()()

' Print the expressions from the block expression.
Console.WriteLine("The expressions from the block expression:")
For Each expr In blockExpr.Expressions
    Console.WriteLine(expr.ToString())
Next

' Print the result of the tree execution.
Console.WriteLine("The return value of the block expression:")
Console.WriteLine(result)

' This code example produces the following output:
'
' The result of executing the expression tree:
' Hello World!

' The expressions from the block expression:
' Write("Hello ")
' WriteLine("World!")
' 42

' The return value of the block expression:
' 42

注解

方法 Block 可用于创建 BlockExpression

属性

名称 说明
CanReduce

指示节点可以缩减为更简单的节点。 如果返回 true,则可以调用 Reduce()以生成化简形式。

(继承自 Expression)
Expressions

获取此块中的表达式。

NodeType

返回此表达式的节点类型。 重写此方法时,扩展节点应返回 Extension

Result

获取此块中的最后一个表达式。

Type

获取表示 Expression 的表达式的静态类型。

Variables

获取在此块中定义的变量。

方法

名称 说明
Accept(ExpressionVisitor)

调度到此节点类型的特定访问方法。 例如,MethodCallExpression调用 .VisitMethodCall(MethodCallExpression)

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Reduce()

将此节点减少为更简单的表达式。 如果 CanReduce 返回 true,则应返回有效的表达式。 此方法可以返回必须自行减少的另一个节点。

(继承自 Expression)
ReduceAndCheck()

将此节点减少为更简单的表达式。 如果 CanReduce 返回 true,则应返回有效的表达式。 此方法可以返回必须自行减少的另一个节点。

(继承自 Expression)
ReduceExtensions()

将表达式减少为已知节点类型(不是扩展节点),或者仅返回表达式(如果它已是已知类型)。

(继承自 Expression)
ToString()

返回文本 Expression表示形式。

(继承自 Expression)
Update(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

创建类似于此表达式的新表达式,但使用提供的子级。 如果所有子级都相同,它将返回此表达式。

VisitChildren(ExpressionVisitor)

减少节点,然后在减少的表达式上调用访问者委托。 如果节点不可减少,该方法将引发异常。

(继承自 Expression)

适用于