Expression.IfThen(Expression, Expression) Metodo

Definizione

Crea un oggetto ConditionalExpression che rappresenta un blocco condizionale con un'istruzione if.

public:
 static System::Linq::Expressions::ConditionalExpression ^ IfThen(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue);
public static System.Linq.Expressions.ConditionalExpression IfThen (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue);
static member IfThen : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.ConditionalExpression
Public Shared Function IfThen (test As Expression, ifTrue As Expression) As ConditionalExpression

Parametri

test
Expression

Oggetto Expression su cui impostare la proprietà Test.

ifTrue
Expression

Oggetto Expression su cui impostare la proprietà IfTrue.

Restituisce

Oggetto ConditionalExpression la cui proprietà NodeType è uguale a Conditional e le cui proprietà Test, IfTrue sono impostate sui valori specificati. La proprietà IfFalse è impostata sull'espressione predefinita e il tipo dell'oggetto ConditionalExpression risultante restituito da questo metodo è Void.

Esempio

Nell'esempio di codice seguente viene illustrato come creare un'espressione che rappresenta un blocco condizionale.

// Add the following directive to the file:
// using System.Linq.Expressions;
bool test = true;

// This expression represents the conditional block.
Expression ifThenExpr = Expression.IfThen(
    Expression.Constant(test),
    Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("The condition is true.")
       )
);

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(ifThenExpr).Compile()();

// This code example produces the following output:
//
// The condition is true.
' Add the following directive to the file:
' Imports System.Linq.Expressions

Dim test As Boolean = True

' This expression represents the conditional block.
Dim ifThenExpr As Expression = Expression.IfThen(
     Expression.Constant(test),
     Expression.Call(
         Nothing,
         GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
         Expression.Constant("The condition is true.")
     )
)

' The following statement first creates an expression tree,
' then compiles it, and then runs it.
Expression.Lambda(Of Action)(ifThenExpr).Compile()()

' This code example produces the following output:
'
' The condition is true.

Si applica a