Expression.IfThen(Expression, Expression) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Crea un objeto ConditionalExpression que representa un bloque condicional con una if instrucción .
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
Parámetros
- test
- Expression
que Expression se va a establecer la Test propiedad igual a.
- ifTrue
- Expression
que Expression se va a establecer la IfTrue propiedad igual a.
Devoluciones
que ConditionalExpression tiene la NodeType propiedad igual a Conditional y las Testpropiedades , IfTrue, establecidas en los valores especificados. La IfFalse propiedad se establece en expresión predeterminada y el tipo del resultado ConditionalExpression devuelto por este método es Void.
Ejemplos
En el ejemplo de código siguiente se muestra cómo crear una expresión que represente un bloque condicional.
// 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.