Expression.IfThenElse(Expression, Expression, Expression) Метод

Определение

Создает объект ConditionalExpression, представляющий условный блок с операторами if и else.

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

Параметры

test
Expression

Объект Expression, который следует задать в качестве значения свойства Test.

ifTrue
Expression

Объект Expression, который следует задать в качестве значения свойства IfTrue.

ifFalse
Expression

Объект Expression, который следует задать в качестве значения свойства IfFalse.

Возвращаемое значение

ConditionalExpression

Выражение ConditionalExpression со свойством NodeType, равным Conditional, и свойствами Test, IfTrue и IfFalse, для которых заданы указанные значения. Тип выражения ConditionalExpression, возвращаемого этим методом, — Void.

Примеры

В следующем примере кода показано, как создать выражение, представляющее условный блок.

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

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

// The following statement first creates an expression tree,
// then compiles it, and then runs it.
Expression.Lambda<Action>(ifThenElseExpr).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 ifThenElseExpr As Expression = Expression.IfThenElse(
     Expression.Constant(test),
     Expression.Call(
         Nothing,
         GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
         Expression.Constant("The condition is true.")
     ),
     Expression.Call(
         Nothing,
         GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)}),
         Expression.Constant("The condition is false.")
     )
)

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

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

Применяется к