Expression.IfThen(Expression, Expression) Methode

Definition

Erstellt einen ConditionalExpression , der einen bedingten Block mit einer if Anweisung darstellt.

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

Parameter

test
Expression

Eine Expression , die die Test Eigenschaft gleich festlegen soll.

ifTrue
Expression

Eine Expression , die die IfTrue Eigenschaft gleich festlegen soll.

Gibt zurück

A ConditionalExpression that has the NodeType property equal to Conditional and the TestIfTrue, properties set to the specified values. Die IfFalse Eigenschaft wird auf den Standardausdruck festgelegt, und der Typ des von dieser Methode zurückgegebenen Ergebnisses ConditionalExpression lautet Void.

Beispiele

Das folgende Codebeispiel zeigt, wie Sie einen Ausdruck erstellen, der einen bedingten Block darstellt.

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

Gilt für: