Expression.IfThen(Expression, Expression) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Skapar en ConditionalExpression som representerar ett villkorsblock med en if instruktion.
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
Parametrar
- test
- Expression
En Expression som egenskapen Test ska vara lika med.
- ifTrue
- Expression
En Expression som egenskapen IfTrue ska vara lika med.
Returer
A ConditionalExpression som har NodeType egenskapen lika med Conditional och Testegenskaperna , IfTrue, inställda på de angivna värdena. Egenskapen IfFalse är inställd på standarduttryck och den typ av resultat som ConditionalExpression returneras av den här metoden är Void.
Exempel
I följande kodexempel visas hur du skapar ett uttryck som representerar ett villkorsblock.
// 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.