Expression.IfThenElse(Expression, Expression, Expression) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
ConditionalExpression Vytvoří objekt, který představuje podmíněný blok s if
příkazy a else
příkazy.
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
Parametry
- test
- Expression
Vlastnost Expression je nastavena na hodnotu rovna Test .
- ifTrue
- Expression
Vlastnost Expression je nastavena na hodnotu rovna IfTrue .
- ifFalse
- Expression
Vlastnost Expression je nastavena na hodnotu rovna IfFalse .
Návraty
AConditionalExpression, který má NodeType vlastnost rovna Conditional a TestIfTruevlastnosti , a IfFalse vlastnosti nastavené na zadané hodnoty. Typ výsledné ConditionalExpression vrácené touto metodou je Void.
Příklady
Následující příklad kódu ukazuje, jak vytvořit výraz, který představuje podmíněný blok.
// 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.