BlockExpression Třída

Definice

Představuje blok, který obsahuje posloupnost výrazů, ve kterých lze definovat proměnné.

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
Dědičnost
BlockExpression

Příklady

Následující příklad kódu ukazuje, jak vytvořit výraz bloku. Výraz bloku se skládá ze dvou MethodCallExpression objektů a jednoho ConstantExpression objektu.

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

Poznámky

Metody Block lze použít k vytvoření objektu BlockExpression.

Vlastnosti

CanReduce

Označuje, že uzel lze zmenšit na jednodušší uzel. Pokud se vrátí hodnota true, lze voláním funkce Reduce() vytvořit redukovanou formu.

(Zděděno od Expression)
Expressions

Získá výrazy v tomto bloku.

NodeType

Vrátí typ uzlu tohoto výrazu. Uzly rozšíření by se měly vrátit Extension při přepsání této metody.

Result

Získá poslední výraz v tomto bloku.

Type

Získá statický typ výrazu, který představuje Expression .

Variables

Získá proměnné definované v tomto bloku.

Metody

Accept(ExpressionVisitor)

Odesílá do konkrétní metody visit pro tento typ uzlu. Například MethodCallExpression volá VisitMethodCall(MethodCallExpression).

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Získá aktuální Type instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
Reduce()

Zmenší tento uzel na jednodušší výraz. Pokud CanReduce vrátí hodnotu true, měl by vrátit platný výraz. Tato metoda může vrátit jiný uzel, který musí být redukován.

(Zděděno od Expression)
ReduceAndCheck()

Zmenší tento uzel na jednodušší výraz. Pokud CanReduce vrátí hodnotu true, měl by vrátit platný výraz. Tato metoda může vrátit jiný uzel, který musí být redukován.

(Zděděno od Expression)
ReduceExtensions()

Zmenšuje výraz na známý typ uzlu (který není uzel rozšíření) nebo pouze vrátí výraz, pokud už je známým typem.

(Zděděno od Expression)
ToString()

Vrátí textovou reprezentaci Expression.

(Zděděno od Expression)
Update(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Vytvoří nový výraz, který je podobný tomuto, ale používá zadané podřízené položky. Pokud jsou všechny podřízené položky stejné, vrátí tento výraz.

VisitChildren(ExpressionVisitor)

Zmenší uzel a pak zavolá delegáta návštěvníka na výraz redukce. Metoda vyvolá výjimku, pokud uzel není reduciovatelný.

(Zděděno od Expression)

Platí pro