Expression.IfThen(Expression, Expression) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Cria um ConditionalExpression que representa um bloco condicional com uma if afirmação.
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
E Expression para definir a Test propriedade igual a .
- ifTrue
- Expression
E Expression para definir a IfTrue propriedade igual a .
Devoluções
A ConditionalExpression que tem a NodeType propriedade igual a Conditional e as Test, IfTrue, propriedades definidas aos valores especificados. A IfFalse propriedade está definida como expressão por defeito e o tipo do resultado ConditionalExpression devolvido por este método é Void.
Exemplos
O seguinte exemplo de código mostra como criar uma expressão que representa um bloco 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.