BlockExpression Klasa

Definicja

Reprezentuje blok zawierający sekwencję wyrażeń, w których można zdefiniować zmienne.

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
Dziedziczenie
BlockExpression

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć wyrażenie blokowe. Wyrażenie blokowe składa się z dwóch MethodCallExpression obiektów i jednego ConstantExpression obiektu.

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

Uwagi

Metody Block można użyć do utworzenia elementu BlockExpression.

Właściwości

CanReduce

Wskazuje, że węzeł można zmniejszyć do prostszego węzła. Jeśli zwraca wartość true, funkcja Reduce() może zostać wywołana w celu utworzenia skróconej postaci.

(Odziedziczone po Expression)
Expressions

Pobiera wyrażenia w tym bloku.

NodeType

Zwraca typ węzła tego wyrażenia. Węzły rozszerzenia powinny być zwracane Extension podczas zastępowania tej metody.

Result

Pobiera ostatnie wyrażenie w tym bloku.

Type

Pobiera statyczny typ wyrażenia, które Expression reprezentuje.

Variables

Pobiera zmienne zdefiniowane w tym bloku.

Metody

Accept(ExpressionVisitor)

Wysyła do określonej metody wizyty dla tego typu węzła. Na przykład MethodCallExpression wywołuje element VisitMethodCall(MethodCallExpression).

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Reduce()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinna zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który musi zostać zmniejszony.

(Odziedziczone po Expression)
ReduceAndCheck()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinna zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który musi zostać zmniejszony.

(Odziedziczone po Expression)
ReduceExtensions()

Zmniejsza wyrażenie do znanego typu węzła (który nie jest węzłem rozszerzenia) lub po prostu zwraca wyrażenie, jeśli jest już znanym typem.

(Odziedziczone po Expression)
ToString()

Zwraca tekstową reprezentację obiektu Expression.

(Odziedziczone po Expression)
Update(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Tworzy nowe wyrażenie, które jest podobne do tego, ale przy użyciu dostarczonych elementów podrzędnych. Jeśli wszystkie elementy podrzędne są takie same, zwróci to wyrażenie.

VisitChildren(ExpressionVisitor)

Zmniejsza węzeł, a następnie wywołuje delegata gościa w wyrażeniu zredukowanym. Metoda zgłasza wyjątek, jeśli węzeł nie jest reducible.

(Odziedziczone po Expression)

Dotyczy