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

次のコード例は、ブロック式を作成する方法を示しています。 ブロック式は、2 つの MethodCallExpression オブジェクトと 1 つの 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)

このノード タイプの特定の visit メソッドにディスパッチします。 たとえば、 MethodCallExpressionVisitMethodCall(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)

適用対象