Expression.Condition 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 en villkorsstyrd instruktion.
Överlagringar
| Name | Description |
|---|---|
| Condition(Expression, Expression, Expression) |
Skapar en ConditionalExpression som representerar en villkorsstyrd instruktion. |
| Condition(Expression, Expression, Expression, Type) |
Skapar en ConditionalExpression som representerar en villkorsstyrd instruktion. |
Condition(Expression, Expression, Expression)
Skapar en ConditionalExpression som representerar en villkorsstyrd instruktion.
public:
static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse);
public static System.Linq.Expressions.ConditionalExpression Condition(System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse 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.
- ifFalse
- Expression
En Expression som egenskapen IfFalse ska vara lika med.
Returer
En ConditionalExpression som har NodeType egenskapen lika med Conditional och Testegenskaperna , IfTrueoch IfFalse som är inställda på de angivna värdena.
Undantag
test eller ifTrue är ifFalsenull.
Exempel
I följande kodexempel visas hur du skapar ett uttryck som representerar en villkorssats. Om det första argumentet utvärderas till truekörs det andra argumentet. Annars körs det tredje argumentet.
// Add the following directive to your file:
// using System.Linq.Expressions;
int num = 100;
// This expression represents a conditional operation.
// It evaluates the test (first expression) and
// executes the iftrue block (second argument) if the test evaluates to true,
// or the iffalse block (third argument) if the test evaluates to false.
Expression conditionExpr = Expression.Condition(
Expression.Constant(num > 10),
Expression.Constant("num is greater than 10"),
Expression.Constant("num is smaller than 10")
);
// Print out the expression.
Console.WriteLine(conditionExpr.ToString());
// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
Expression.Lambda<Func<string>>(conditionExpr).Compile()());
// This code example produces the following output:
//
// IIF("True", "num is greater than 10", "num is smaller than 10")
// num is greater than 10
' Add the following directive to your file:
' Imports System.Linq.Expressions
Dim num As Integer = 100
' This expression represents a conditional operation;
' it will evaluate the test (first expression) and
' execute the ifTrue block (second argument) if the test evaluates to true,
' or the ifFalse block (third argument) if the test evaluates to false.
Dim conditionExpr As Expression = Expression.Condition(
Expression.Constant(num > 10),
Expression.Constant("n is greater than 10"),
Expression.Constant("n is smaller than 10")
)
' Print the expression.
Console.WriteLine(conditionExpr.ToString())
' The following statement first creates an expression tree,
' then compiles it, and then executes it.
Console.WriteLine(
Expression.Lambda(Of Func(Of String))(conditionExpr).Compile()())
' This code example produces the following output:
'
' IIF("True", "num is greater than 10", "num is smaller than 10")
' num is greater than 10
Kommentarer
Egenskapen Type för resultatet ConditionalExpression är lika med Type egenskapen ifTrueför .
Se även
Gäller för
Condition(Expression, Expression, Expression, Type)
Skapar en ConditionalExpression som representerar en villkorsstyrd instruktion.
public:
static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse, Type ^ type);
public static System.Linq.Expressions.ConditionalExpression Condition(System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse, Type type);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression, type As Type) 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.
- ifFalse
- Expression
En Expression som egenskapen IfFalse ska vara lika med.
Returer
En ConditionalExpression som har NodeType egenskapen lika med Conditional och Testegenskaperna , IfTrueoch IfFalse som är inställda på de angivna värdena.
Kommentarer
Med den här metoden kan du explicit ena resultattypen för villkorsuttrycket i fall där typerna av ifTrue och ifFalse uttrycken inte är lika. Typer av båda ifTrue och ifFalse måste implicit referera till tilldelningsbara till resultattypen. Tillåts type vara Void.