다음을 통해 공유


Expression.IfThen(Expression, Expression) 메서드

정의

ConditionalExpression 문을 사용하여 조건부 블록을 나타내는 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

매개 변수

test
Expression

Expression 속성에 설정할 Test입니다.

ifTrue
Expression

Expression 속성에 설정할 IfTrue입니다.

반환

ConditionalExpression 속성이 NodeType이고 ConditionalTest 속성이 지정된 값으로 설정된 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 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.

적용 대상