다음을 통해 공유


Expression.Block Method (array<Expression[])

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a BlockExpression that contains the given expressions and has no variables.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function Block ( _
    ParamArray expressions As Expression() _
) As BlockExpression
public static BlockExpression Block(
    params Expression[] expressions
)

Parameters

Remarks

When the block expression is executed, it returns the value of the last expression in the block.

Examples

The following code example shows how to create a block expression. The block expression consists of two MethodCallExpression objects and one ConstantExpression object.

' 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)
)

outputBlock.Text &= "The result of executing the expression tree:" & vbCrLf
' 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.
outputBlock.Text &= "The expressions from the block expression:" & vbCrLf
For Each expr In blockExpr.Expressions
    outputBlock.Text &= expr.ToString() & vbCrLf
Next

' Print the result of the tree execution.
outputBlock.Text &= "The return value of the block expression:" & vbCrLf
outputBlock.Text &= result & vbCrLf

' 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:
// 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)
);

outputBlock.Text += "The result of executing the expression tree:" + "\n";
// 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.
outputBlock.Text += "The expressions from the block expression:" + "\n";
foreach (var expr in blockExpr.Expressions)
   outputBlock.Text += expr.ToString() + "\n";

// Print out the result of the tree execution.
outputBlock.Text += "The return value of the block expression:" + "\n";
outputBlock.Text += result + "\n";

// 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

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.