Expression.Block Méthode

Définition

Crée un BlockExpression.Creates a BlockExpression.

Surcharges

Block(Type, IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

Block(Expression, Expression, Expression)

Crée un BlockExpression qui contient trois expressions et aucune variable.Creates a BlockExpression that contains three expressions and has no variables.

Block(Expression, Expression, Expression, Expression, Expression)

Crée un BlockExpression qui contient deux expressions et aucune variable.Creates a BlockExpression that contains five expressions and has no variables.

Block(Type, IEnumerable<ParameterExpression>, Expression[])

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

Block(Type, Expression[])

Crée un BlockExpression qui contient les expressions données, aucune variable et a un type de résultat spécifique.Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

Block(Expression, Expression, Expression, Expression)

Crée un BlockExpression qui contient quatre expressions et aucune variable.Creates a BlockExpression that contains four expressions and has no variables.

Block(Expression, Expression)

Crée un BlockExpression qui contient deux expressions et aucune variable.Creates a BlockExpression that contains two expressions and has no variables.

Block(IEnumerable<ParameterExpression>, Expression[])

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

Block(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

Block(Expression[])

Crée un BlockExpression qui contient les expressions données et aucune variable.Creates a BlockExpression that contains the given expressions and has no variables.

Block(IEnumerable<Expression>)

Crée un BlockExpression qui contient les expressions données et aucune variable.Creates a BlockExpression that contains the given expressions and has no variables.

Block(Type, IEnumerable<Expression>)

Crée un BlockExpression qui contient les expressions données, aucune variable et a un type de résultat spécifique.Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

Block(Type, IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(Type ^ type, System::Collections::Generic::IEnumerable<System::Linq::Expressions::ParameterExpression ^> ^ variables, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);
static member Block : Type * seq<System.Linq.Expressions.ParameterExpression> * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.BlockExpression

Paramètres

type
Type

Type de résultat du bloc.The result type of the block.

variables
IEnumerable<ParameterExpression>

Variables contenues dans le bloc.The variables in the block.

expressions
IEnumerable<Expression>

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Block(Expression, Expression, Expression)

Crée un BlockExpression qui contient trois expressions et aucune variable.Creates a BlockExpression that contains three expressions and has no variables.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Linq::Expressions::Expression ^ arg0, System::Linq::Expressions::Expression ^ arg1, System::Linq::Expressions::Expression ^ arg2);
public static System.Linq.Expressions.BlockExpression Block (System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2);
static member Block : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (arg0 As Expression, arg1 As Expression, arg2 As Expression) As BlockExpression

Paramètres

arg0
Expression

Première expression contenue dans le bloc.The first expression in the block.

arg1
Expression

Deuxième expression contenue dans le bloc.The second expression in the block.

arg2
Expression

Troisième expression contenue dans le bloc.The third expression in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(Expression, Expression, Expression, Expression, Expression)

Crée un BlockExpression qui contient deux expressions et aucune variable.Creates a BlockExpression that contains five expressions and has no variables.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Linq::Expressions::Expression ^ arg0, System::Linq::Expressions::Expression ^ arg1, System::Linq::Expressions::Expression ^ arg2, System::Linq::Expressions::Expression ^ arg3, System::Linq::Expressions::Expression ^ arg4);
public static System.Linq.Expressions.BlockExpression Block (System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2, System.Linq.Expressions.Expression arg3, System.Linq.Expressions.Expression arg4);
static member Block : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (arg0 As Expression, arg1 As Expression, arg2 As Expression, arg3 As Expression, arg4 As Expression) As BlockExpression

Paramètres

arg0
Expression

Première expression contenue dans le bloc.The first expression in the block.

arg1
Expression

Deuxième expression contenue dans le bloc.The second expression in the block.

arg2
Expression

Troisième expression contenue dans le bloc.The third expression in the block.

arg3
Expression

Quatrième expression contenue dans le bloc.The fourth expression in the block.

arg4
Expression

Cinquième expression contenue dans le bloc.The fifth expression in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(Type, IEnumerable<ParameterExpression>, Expression[])

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(Type ^ type, System::Collections::Generic::IEnumerable<System::Linq::Expressions::ParameterExpression ^> ^ variables, ... cli::array <System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, params System.Linq.Expressions.Expression[] expressions);
static member Block : Type * seq<System.Linq.Expressions.ParameterExpression> * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.BlockExpression

Paramètres

type
Type

Type de résultat du bloc.The result type of the block.

variables
IEnumerable<ParameterExpression>

Variables contenues dans le bloc.The variables in the block.

expressions
Expression[]

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Block(Type, Expression[])

Crée un BlockExpression qui contient les expressions données, aucune variable et a un type de résultat spécifique.Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(Type ^ type, ... cli::array <System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (Type type, params System.Linq.Expressions.Expression[] expressions);
static member Block : Type * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.BlockExpression

Paramètres

type
Type

Type de résultat du bloc.The result type of the block.

expressions
Expression[]

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Block(Expression, Expression, Expression, Expression)

Crée un BlockExpression qui contient quatre expressions et aucune variable.Creates a BlockExpression that contains four expressions and has no variables.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Linq::Expressions::Expression ^ arg0, System::Linq::Expressions::Expression ^ arg1, System::Linq::Expressions::Expression ^ arg2, System::Linq::Expressions::Expression ^ arg3);
public static System.Linq.Expressions.BlockExpression Block (System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1, System.Linq.Expressions.Expression arg2, System.Linq.Expressions.Expression arg3);
static member Block : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (arg0 As Expression, arg1 As Expression, arg2 As Expression, arg3 As Expression) As BlockExpression

Paramètres

arg0
Expression

Première expression contenue dans le bloc.The first expression in the block.

arg1
Expression

Deuxième expression contenue dans le bloc.The second expression in the block.

arg2
Expression

Troisième expression contenue dans le bloc.The third expression in the block.

arg3
Expression

Quatrième expression contenue dans le bloc.The fourth expression in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(Expression, Expression)

Crée un BlockExpression qui contient deux expressions et aucune variable.Creates a BlockExpression that contains two expressions and has no variables.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Linq::Expressions::Expression ^ arg0, System::Linq::Expressions::Expression ^ arg1);
public static System.Linq.Expressions.BlockExpression Block (System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1);
static member Block : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (arg0 As Expression, arg1 As Expression) As BlockExpression

Paramètres

arg0
Expression

Première expression contenue dans le bloc.The first expression in the block.

arg1
Expression

Deuxième expression contenue dans le bloc.The second expression in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(IEnumerable<ParameterExpression>, Expression[])

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Collections::Generic::IEnumerable<System::Linq::Expressions::ParameterExpression ^> ^ variables, ... cli::array <System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, params System.Linq.Expressions.Expression[] expressions);
static member Block : seq<System.Linq.Expressions.ParameterExpression> * System.Linq.Expressions.Expression[] -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (variables As IEnumerable(Of ParameterExpression), ParamArray expressions As Expression()) As BlockExpression

Paramètres

variables
IEnumerable<ParameterExpression>

Variables contenues dans le bloc.The variables in the block.

expressions
Expression[]

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(IEnumerable<ParameterExpression>, IEnumerable<Expression>)

Crée un BlockExpression qui contient les variables et expressions données.Creates a BlockExpression that contains the given variables and expressions.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Collections::Generic::IEnumerable<System::Linq::Expressions::ParameterExpression ^> ^ variables, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (System.Collections.Generic.IEnumerable<System.Linq.Expressions.ParameterExpression> variables, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);
static member Block : seq<System.Linq.Expressions.ParameterExpression> * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (variables As IEnumerable(Of ParameterExpression), expressions As IEnumerable(Of Expression)) As BlockExpression

Paramètres

variables
IEnumerable<ParameterExpression>

Variables contenues dans le bloc.The variables in the block.

expressions
IEnumerable<Expression>

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Exemples

L’exemple de code suivant montre comment passer un paramètre à une expression de bloc et traiter ce paramètre dans un bloc.The following code example shows how to pass a parameter to a block expression and process this parameter within a block.

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


// This block has a parameter expression
// that represents a variable within the block scope.
// It assigns a value to the variable,
// and then adds a constant to the assigned value. 

ParameterExpression varExpr = Expression.Variable(typeof(int), "sampleVar");
BlockExpression blockExpr = Expression.Block(
    new ParameterExpression[] { varExpr },
    Expression.Assign(varExpr, Expression.Constant(1)),
    Expression.Add(varExpr, Expression.Constant(5))
);

// 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());

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

// This code example produces the following output:
// The expressions from the block expression:
// (sampleVar = 1)
// (sampleVar + 5)
// The result of executing the expression tree:
// 6

' Add the following directive to your file:
' Imports System.Linq.Expressions  


' This block has a parameter expression
' that represents a variable within the block scope.
' It assigns a value to the variable,
' and then adds a constant to the assigned value. 

Dim varExpr As ParameterExpression = Expression.Variable(GetType(Integer), "sampleVar")
Dim blockExpr As BlockExpression = Expression.Block(
    New ParameterExpression() {varExpr},
    Expression.Assign(varExpr, Expression.Constant(1)),
    Expression.Add(varExpr, Expression.Constant(5))
)

' 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

Console.WriteLine("The result of executing the expression tree:")

' The following statement first creates an expression tree,
' then compiles it, and then executes it.
Console.WriteLine(
    Expression.Lambda(Of Func(Of Integer))(blockExpr).Compile()())

' This code example produces the following output:
'
' The expressions from the block expression:
' (sampleVar = 1)
' (sampleVar + 5)
' The result of executing the expression tree:
' 6

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(Expression[])

Crée un BlockExpression qui contient les expressions données et aucune variable.Creates a BlockExpression that contains the given expressions and has no variables.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(... cli::array <System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (params System.Linq.Expressions.Expression[] expressions);
static member Block : System.Linq.Expressions.Expression[] -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (ParamArray expressions As Expression()) As BlockExpression

Paramètres

expressions
Expression[]

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Exemples

L’exemple de code suivant montre comment créer une expression de bloc.The following code example shows how to create a block expression. L’expression de bloc se compose MethodCallExpression de deux objets ConstantExpression et d’un objet.The block expression consists of two MethodCallExpression objects and one ConstantExpression object.

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

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(IEnumerable<Expression>)

Crée un BlockExpression qui contient les expressions données et aucune variable.Creates a BlockExpression that contains the given expressions and has no variables.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);
static member Block : seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.BlockExpression
Public Shared Function Block (expressions As IEnumerable(Of Expression)) As BlockExpression

Paramètres

expressions
IEnumerable<Expression>

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

Remarques

Lorsque l’expression de bloc est exécutée, elle retourne la valeur de la dernière expression dans le bloc.When the block expression is executed, it returns the value of the last expression in the block.

Block(Type, IEnumerable<Expression>)

Crée un BlockExpression qui contient les expressions données, aucune variable et a un type de résultat spécifique.Creates a BlockExpression that contains the given expressions, has no variables and has specific result type.

public:
 static System::Linq::Expressions::BlockExpression ^ Block(Type ^ type, System::Collections::Generic::IEnumerable<System::Linq::Expressions::Expression ^> ^ expressions);
public static System.Linq.Expressions.BlockExpression Block (Type type, System.Collections.Generic.IEnumerable<System.Linq.Expressions.Expression> expressions);
static member Block : Type * seq<System.Linq.Expressions.Expression> -> System.Linq.Expressions.BlockExpression

Paramètres

type
Type

Type de résultat du bloc.The result type of the block.

expressions
IEnumerable<Expression>

Expressions contenues dans le bloc.The expressions in the block.

Retours

Élément BlockExpression créé.The created BlockExpression.

S’applique à