Expression.IfThenElse(Expression, Expression, Expression) Expression.IfThenElse(Expression, Expression, Expression) Expression.IfThenElse(Expression, Expression, Expression) Expression.IfThenElse(Expression, Expression, Expression) Method

定義

ConditionalExpression ステートメントと if ステートメントを指定して、条件付きブロックを表す else を作成します。Creates a ConditionalExpression that represents a conditional block with if and else statements.

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

Expression プロパティを等しく設定する TestAn Expression to set the Test property equal to.

ifTrue
Expression Expression Expression Expression

Expression プロパティを等しく設定する IfTrueAn Expression to set the IfTrue property equal to.

ifFalse
Expression Expression Expression Expression

Expression プロパティを等しく設定する IfFalseAn Expression to set the IfFalse property equal to.

戻り値

ConditionalExpression と等しい NodeType プロパティと、指定した値に設定された ConditionalTest、および IfTrue の各プロパティを含む IfFalseA ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values. このメソッドによって返される結果の ConditionalExpression の型は Void です。The type of the resulting ConditionalExpression returned by this method is Void.

次のコード例では、条件付きブロックを表す式を作成する方法を示します。The following code example shows how to create an expression that represents a conditional block.

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

適用対象